Print Friendly and PDF
binary to decimal conversion This C++ Program converts the given binary number into decimal. The program reads the binary number, does a modulo operation to get the remainder, multiples the total by base 2and adds the modulo and repeats the steps.
This C++ Program converts the given binary number into decimal. The program reads the binary number, does a modulo operation to get the remainder, multiples the total by base 2and adds the modulo and repeats the steps.

Here is source code of the C++ program which converts the given binary number into decimal. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

Example

#include <iostream>
using namespace std;
int main()
{
int binary, number, decimal = 0, highBit = 16, factor = 10000;

cout << "Enter a Binary Number [5 Digits maximum]: "<<endl;
cin >> binary;
nu mber = binary;


while ( highBit >= 1 ) 
{
decimal += binary / factor * highBit;
highBit /= 2;
binary %= factor;
factor /= 10;
}

cout << "The decimal equivalent of "<< number << " is " << decimal << endl;
return 0;
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: