Skip to main content

Program to convert Binary to Decimal to Binary.

 Program to convert Binary to Decimal


CODE

👇

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<conio.h>

  4. int binary_decimal(int n);

  5. void main()
  6. {
  7.     int n;
  8.     char c;
  9.     printf("Enter Binary number :  ");
  10.     scanf("%d", &n);
  11.     printf("%d in binary = %d in decimal", n, binary_decimal(n));
  12.     getch();
  13. }

  14. //Function to convert binary to decimal.

  15. int binary_decimal(int n)
  16. {
  17.     int decimal = 0, i = 0, rem;
  18.     while (n != 0)
  19.     {
  20.         rem = n % 10;
  21.         n /= 10;
  22.         decimal += rem * pow(2, i);
  23.         ++i;
  24.     }
  25.     return decimal;
  26. }

👉Execut👈
//OUTPUT
/*
Enter Binary number :  10110111
10110111 in binary = 183 in decimal
*/


Program to convert Decimal numbers to Binary.


CODE
👇
  1. #include<stdio.h>
  2. #include<conio.h>

  3. void main()
  4. {
  5.     int n, c, k;
  6.     printf("Enter a decimal number : ");
  7.     scanf("%d", &n);

  8.     printf("\n%d in binary number system is : ", n);

  9.     for (c = 8; c >= 0; c--)
  10.     {
  11.         k = n >> c;
  12.         if (k & 1)
  13.         {
  14.             printf("1");
  15.         }
  16.         else
  17.         {
  18.             printf("0");
  19.         }
  20.     }

  21.     printf("\n");
  22. }


👉Execute👈

//OUTPUT
/*
Enter a decimal number : 143

143 in binary number system is : 010001111

*/







                                                                                   //ThE ProFessoR