C Program to Convert Binary Number to Decimal


This program converts binary number to equivalent decimal number.

  1. //C program to convert binary to decimal
  2. #include <stdio.h>
  3. #include <math.h>
  4. int convert(long long n);
  5. int main()
  6. {
  7. long long n;
  8. printf("Enter a binary number: ");
  9. scanf("%d", &n);
  10. printf("%d in binary = %d in decimal", n, convert(n));
  11. return 0;
  12. }
  13. int convert(long long n)
  14. {
  15. int decim = 0, i = 0, remr;
  16. while (n!=0)
  17. {
  18. remr = n%10;
  19. n /= 10;
  20. decim += remr*pow(2,i);
  21. ++i;
  22. }
  23. return decim;
  24. }

Output

Output!