Binary To Decimal C++: Program to Convert Binary to Decimal


C++ is one of the principal programming languages as it is the necessary step to learn how to ‘code’. Students are expected to learn C++ thoroughly before other programming languages such as Java, CSS, etc., are introduced through their curriculum. Coding in C++ primarily involves using functions, classes, processing strings and arrays of data input and output. 

Decimal numbers are the common integers that we use in our daily lives. The decimal numbers have a base of 10, as 10 possible digits are ranging from 0,1,2 … 9. Decimal numbers are convenient for usage in our daily lives, but a machine/computer cannot directly process decimal numbers through its CPU. Binary numbers are a set of 0s and 1s that can be processed by the computer system, as it forms the machine language.

These numbers are arranged in unique configurations that correlate to numbers, letters, special characters, or other fundamental information. The base of these binary numbers is ‘2’, as the only digits it contains are 0 and 1. For eg. 0010 (Binary number) = 2 (Decimal number).

When the computer performs calculations within its system, the binary number thus calculated needs to be displayed to the user through a decimal number. For this purpose, a C++ program that can convert a binary number into a decimal number is used, as displayed below.

C++ Program 1

Step 1. #include<iostream.h>
Step 2. using namespace std;
Step 3. int main()
Step 4.{
Step 5. int num, temp, rem, dec = 0, b = 1; 
Step 6.cout << “Enter Binary Number – ”;
Step 7.cin >> num
Step 8.temp >> num
Step 9.while (num > 0)
Step 10.{
Step 11.rem = temp % 10
Step 12.dec = dec + rem * 2
Step 13.b =*2;
Step 14.temp /= 10;
Step 15.}
Step 16.cout << “The decimal conversion of “ << num << “is” << dec;
Step 17.return 0;
Step 18. }

The above C++ program will effectively convert a binary number (up to 31) into a decimal number. To convert binary numbers larger than 31 into decimal numbers, a string needs to be initialized, along with the while loop.

C++ Program 2

Step 1. #include<iostream.h>
Step 2. #include<string.h>
Step 3. using namespace std;
Step binarytodecimal(string n)
Step 5. {
Step 6.string num = n;
Step dec_value = 0;
Step base = 1;
Step len = num.length( );
Step 10.for (int i = len – 1; i >= 0; i–)
Step 11.{
Step 12.while (num > 0)
Step 13.{
Step 14.  if (num[i] == ‘1’)
Step 15. dec_value += base;
Step 16. base = base * 2;
Step 17.}
Step 18. return dec_value;
Step 19}
Step 20int main( )
Step 21{
Step 22string num = “10101000”;
Step 23cout << binarytodecimal(num) << endl;
Step 24}

The output of the above code will be displayed as follows: “168”.

Thus, we can convert a binary number into a decimal number in the C++ programming interface by utilising the code in two different methods. Application of the given C++ program includes the display of a decimal number on the computer screen after the ALU performs mathematical calculations, as requested by the user. Since the computer processes data in ‘bits’, as series of Ones and Zeros, the processed data must be converted into decimal numbers for the user’s understanding and comprehension.

To convert decimal numbers into binary numbers in the C++ programming interface, the following code must be used:

Also Read: C++ Project Ideas & Topics

C++ Program 3

Step 1. #include<iostream.h>
Step 2. using namespace std;
Step 3. void decToBinary (int n)
Step 4.{
Step 5. int binaryNu[32];
Step i = 0;
Step 7.while (n > 0)
Step 8.{
Step 9.binaryNum[ i ] = n % 2;
Step 10.n = n / 2;
Step 11.i++;
Step 12.}
Step 13.for 
Step 14. int (j = i – 1, j > 0; j — )
Step 15.cout << binaryNum [ j ];
Step 16. base = base * 2;
Step 17.}
Step 18. int main ( )
Step 19{
Step 20int n = 18;
Step 21decToBinary (n);
Step 22return 0;
Step 23}

The output of the above code will be displayed as “10010”, for an entered input of “18”.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.


These C++ programming codes are some of the elementary programs taught in the introduction sections of programming courses. upGrad hosts a range of courses for the students who want to understand programming in-depth.

If you are interested to learn more and need mentorship from industry experts, check out upGrad & IIIT Banglore’s Executive PG Programme in Full-Stack Software Development.

Land on Your Dream Job

0 replies on “Binary To Decimal C++: Program to Convert Binary to Decimal”

Let’s do it!
No, thanks.