Q:

Write C++ program to convert binary number to decimal

0

Write C++ program to convert binary number to decimal

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

#include <iostream>
#include <math.h>
using namespace std;
 
//Function declartion
int convertBinaryToDecimal(long long n);
 
int main()
{
    long long n;
    cout<<"Enter a binary number: ";
    // Inputting number from user
    cin>>n;
    //Printing binary number to decimal
    cout<<n<<" in binary = "<<convertBinaryToDecimal(n)<<" in decimal";
    return 0;
}
 
//Function to convert binary number to decimal
int convertBinaryToDecimal(long long n)
{
    int decimalNumber = 0, i = 0, remainder;
    while (n!=0)
    {
        remainder = n%10;
        n /= 10;
        decimalNumber += remainder*pow(2,i);
        ++i;
    }
    return decimalNumber;
}

Result:

Enter a binary number: 1000

1000 in binary = 8 in decimal

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C++ program to convert decimal number to bin... >>
<< Write C++ program to find cube of a number using f...