Write C++ program to convert binary number to decimal


I have used CodeBlocks 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

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


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

Result

C++ language techstudy.org

     





Related Topics




Popular Posts