Write C Program to convert binary number to decimal


Introduction

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Write C Program to convert binary number to decimal


  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. //Function declartion
  5. int convertBinaryToDecimal(long long n);
  6.  
  7. int main()
  8. {
  9. long long n;
  10. printf("Enter a binary number: ");
  11. // Inputting number from user
  12. scanf("%lld", &n);
  13. //Printing binary number to decimal
  14. printf("%lld in binary = %d in decimal", n, convertBinaryToDecimal(n));
  15. return 0;
  16. }
  17.  
  18. int convertBinaryToDecimal(long long n)
  19. {
  20. int decimalNumber = 0, i = 0, remainder;
  21. while (n!=0)
  22. {
  23. remainder = n%10;
  24. n /= 10;
  25. decimalNumber += remainder*pow(2,i);
  26. ++i;
  27. }
  28. return decimalNumber;
  29. }
  30.  

Result

C language

     





Related Topics




Popular Posts