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!
download android app

Popular C Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube