Binary number as input, we need to write a program to convert the given binary number into equivalent decimal number. Binary to Decimal program in C++ First, extract digits from the right side of the number. The extracted digit is then multiplied by the proper base (power of 2).
Convert Binary to Decimal in C++ Loops
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
unsigned long i,n,num=0,d;
cout<<"Enter any Binary number:";
cin>>n;
cout<<"\nThe Decimal conversion of "<<n<<" is ";
for(i=0;n!=0;++i)
{
d=n%10;
num=(d)*(pow(2,i))+num;
n=n/10;
}
cout<<num;
return 0;
}
Output of Program
Enter any Binary number:5
The Decimal conversion of 5 is 5