# How to convert binary to decimal in c++

Welcome to the new article on how to convert binary to decimal in c++. This article will discuss converting binary (base 2) numbers to decimal numbers (base 10). First, we will understand the algorithm pictorially. Then we will also write c++ code to elevate our understanding of the concept. Let’s start understanding how to convert binary to decimal in c++.

## Why binary and its conversion is important?

We are familiar with the importance of binary numbers as well as their conversion in computer science. One importance of binary numbers is that it simplifies the design of the computers. We know that data is stored on the computer in the form of bits and bytes. A bit is a digit that can be either 1 or 0 like a switch that is either on or off. A group of 8 bits is called byte. On computers, the data is actually stored in binary, but it is served to the user in its original form after performing conversion operations.

## How to convert binary to decimal?

The below figure can be used to understand how binary is converted in decimal.
The method of conversion is divided into three steps and the final answer is 37. The explanation of all three steps is as follows.
1. In the first step, we separate the rightmost digit from the right side. Each separated digit is written in a separate box in the above figure.
2. Each separated digit is then multiplied with the power of 2 in sequence. The power is decided according to the position of the digit. If the digit is the rightmost then it is multiplied by 20. It is multiplied with 21 when the digit is a second rightmost and then the third rightmost digit is multiplied with 22 and so on.
3. All the non-zero digits resulted in non-zero values and zeros are still zeros which are the result of the second step. The result of all the separated digits is added in the third step. The final answer is 37 in decimal which is the result of the third step.

That’s how we convert binary to decimal. Now let’s convert the above algorithm into c++ code.

## c++ program to convert binary to decimal

```#include <iostream>
using namespace std;
int main()
{//--------Declaring variables-----------
int num;
int AnsInDecimal = 0;
int base = 1;//bcz 2^0=1 for first iteration
int temp;
//--------Taking input--------------------
cin >> num;
temp = num;
//---Loop to seperate rightmost digit-----
while (temp) {
int last_digit = temp % 10;// seperating last digit
temp = temp / 10;//removing last digit from binary num
AnsInDecimal = AnsInDecimal + last_digit * base;
base = base * 2;//calculating base for next iteration
}
cout << num<<" In decimal is = "AnsInDecimal << endl;
system("pause");
return 0;
}```

The above code is divided into four parts by comments. Let’s understand each of these parts stepwise.

The first part:

• In the first part, we declared different variables for different purposes.
• We have taken num to take a binary number as input from the user, AnsIndecimal to store converted decimal number, base to store the answers of powers of 2, and temp to temporarily store num which helps keep num unchanged.
• We initialized AnsInDecimal with 0. Also, we initialized the base with 0 because 2 power 0 is equal to 1.

The second part:

•   We have just taken input from the user in the second part and copied it into the temporary variable temp.
• Let’s say the user entered 100101. So, the temp has 100101.

The  third part:

• The third part is the main and logical part. We run a while loop with temp which means it will iterate until temp has a non-zero value.
• We took modulus % of temp with 10 to separate the last digit of the binary number and then stored it in the local variable last_digit. last_digit has 1 stored after taking the modulus of temp.
• After taking modulus, temp is divided with 10 to remove the last digit as it is already separated and stored in last_digit. Now temp has 10010 and the last digit is removed.
•  After the removal of the last digit, ansIndecimal is calculated by multiplying base with last_digit and then adding it with ansIndecimal. It is the final product of the first iteration.
• Now the value base is multiplied by 2 to get the next power of 2.
• In this way, the first iteration is executed. The rest of the iteration executes in the same way.

The fourth part:

• After the execution of all six iterations, AnsIndecimal has the final answer which is displayed in the last part of the code.
• We also displayed num, the user input in binary, in the cout statement. For this reason, we used an extra variable temp to keep the value of num unchanged.
That’s it on how to convert binary to decimal in c++. In the coming articles, we will also discuss other conversions like decimal to binary, etc  Thanks for reading and supporting us.
Scroll to Top