Convert Binary to Decimal

C++ Program to Convert Binary to Decimal (5 Ways With Output)

IntermediateTopic: Advanced Number Programs
Back

C++ Convert Binary to Decimal Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    long long binary;
    int decimal = 0, i = 0, remainder;
    
    cout << "Enter a binary number: ";
    cin >> binary;
    
    long long temp = binary;
    
    while (temp != 0) {
        remainder = temp % 10;
        temp /= 10;
        decimal += remainder * pow(2, i);
        ++i;
    }
    
    cout << "Binary: " << binary << " = Decimal: " << decimal << endl;
    
    return 0;
}
Output
Enter a binary number: 1010
Binary: 1010 = Decimal: 10

Understanding Convert Binary to Decimal

This program teaches you how to convert a binary number to its decimal (base-10) equivalent in C++. Binary is the base-2 number system used by computers, where each digit can only be 0 or 1. Understanding binary-to-decimal conversion is fundamental for computer science, digital systems, and low-level programming.

---

1. What This Program Does

The program converts a binary number (base-2) to a decimal number (base-10). For example:

Input binary: 1010
Output decimal: 10

Binary numbers use only digits 0 and 1, while decimal numbers use digits 0-9. The conversion involves multiplying each binary digit by 2 raised to its position power and summing the results.

Example:

1010 (binary) = 1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 8 + 0 + 2 + 0 = 10 (decimal)
1101 (binary) = 1×2³ + 1×2² + 0×2¹ + 1×2⁰ = 8 + 4 + 0 + 1 = 13 (decimal)

---

2. Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <cmath>
Provides pow() function for calculating powers.
Essential for computing 2 raised to various powers.

---

3. Declaring Variables

The program declares:

long long binary;

int decimal = 0, i = 0, remainder;

binary stores the binary number entered by the user (long long for large binary numbers).
decimal stores the converted decimal result (initialized to 0).
i tracks the position/index of the current digit (starts at 0 for rightmost digit).
remainder stores the last digit extracted in each iteration.

---

4. Taking Input From the User

The program asks:

cin >> binary;

The user enters a binary number, for example: 1010

We also create a temporary copy:

long long temp = binary;

This is important because we'll modify the number during conversion, and we need the original for display.

---

cout << "Enter a binary number: ";

5. The Conversion Algorithm

The core conversion uses a while loop:

while (temp != 0) {

remainder = temp % 10;

temp /= 10;

decimal += remainder * pow(2, i);

++i;

}

Let's break down how this works step-by-step for binary = 1010:

Step 1:

remainder = 1010 % 10 = 0 (extract rightmost digit)
temp = 1010 / 10 = 101 (remove rightmost digit)
decimal = 0 + 0 × 2⁰ = 0 + 0 = 0
i = 1 (move to next position)

Step 2:

remainder = 101 % 10 = 1
temp = 101 / 10 = 10
decimal = 0 + 1 × 2¹ = 0 + 2 = 2
i = 2

Step 3:

remainder = 10 % 10 = 0
temp = 10 / 10 = 1
decimal = 2 + 0 × 2² = 2 + 0 = 2
i = 3

Step 4:

remainder = 1 % 10 = 1
temp = 1 / 10 = 0
decimal = 2 + 1 × 2³ = 2 + 8 = 10
i = 4

Step 5:

temp is now 0, so the loop stops
Final result: decimal = 10

---

6. Understanding the Binary System

Positional Value System

:

In binary, each position represents a power of 2:

Rightmost position (position 0): 2⁰ = 1
Next position (position 1): 2¹ = 2
Next position (position 2): 2² = 4
Next position (position 3): 2³ = 8
And so on...

Reading Binary from Right to Left

:

Position 0 (rightmost): units place (2⁰ = 1)
Position 1: twos place (2¹ = 2)
Position 2: fours place (2² = 4)
Position 3: eights place (2³ = 8)

Example Calculation

:

Binary 1010:

Digit at position 0: 0 → contributes 0 × 1 = 0
Digit at position 1: 1 → contributes 1 × 2 = 2
Digit at position 2: 0 → contributes 0 × 4 = 0
Digit at position 3: 1 → contributes 1 × 8 = 8
Total: 0 + 2 + 0 + 8 = 10

---

7. Key Operations Explained

Modulo Operator (%)

:

temp % 10 extracts the rightmost digit.
Example: 1010 % 10 = 0, 101 % 10 = 1

Integer Division (/)

:

temp / 10 removes the rightmost digit.
Example: 1010 / 10 = 101, 101 / 10 = 10

Power Function (pow())

:

pow(2, i) calculates 2 raised to the power i.
Example: pow(2, 0) = 1, pow(2, 1) = 2, pow(2, 3) = 8

Accumulation

:

decimal += remainder * pow(2, i) adds the contribution of current digit.
Each digit's contribution is: digit × 2^position

---

8. Other Methods (Mentioned but not shown in code)

Method 2: Using Bit Manipulation

int decimal = 0;

int base = 1;

while (binary > 0) {

int lastDigit = binary % 10;

decimal += lastDigit * base;

base *= 2;

binary /= 10;

}

Uses base variable that doubles each iteration (1, 2, 4, 8, ...).
Avoids pow() function calls - more efficient.

Method 3: Using String

string binaryStr = to_string(binary);

int decimal = 0;

for (int i = 0; i < binaryStr.length(); i++) {

int digit = binaryStr[i] - '0';

decimal += digit * pow(2, binaryStr.length() - 1 - i);

}

Converts binary to string.
Processes each character from left to right.

Method 4: Using Recursion

int binaryToDecimal(long long binary, int i = 0) {

if (binary == 0) return 0;

}

Recursive approach - function calls itself.
Base case: when binary becomes 0.
    return (binary % 10) * pow(2, i) + binaryToDecimal(binary / 10, i + 1);

Method 5: Using stoi() with Base 2

string binaryStr = to_string(binary);

int decimal = stoi(binaryStr, nullptr, 2);

stoi() can convert string to integer with specified base.
Base 2 means binary conversion.
Simplest method but requires string conversion.

---

9. Displaying the Result

The program prints:

Output:

Binary: 1010 = Decimal: 10

This clearly shows both the original binary number and its decimal equivalent.

---

cout << "Binary: " << binary << " = Decimal: " << decimal << endl;

10. Understanding Number Bases

Decimal (Base-10)

:

Uses digits 0-9.
Each position is a power of 10.
Example: 123 = 1×10² + 2×10¹ + 3×10⁰

Binary (Base-2)

:

Uses digits 0-1 only.
Each position is a power of 2.
Example: 1010 = 1×2³ + 0×2² + 1×2¹ + 0×2⁰

Why Binary?

:

Computers use binary because electronic circuits have two states: on (1) and off (0).
All data in computers is ultimately stored in binary format.
Understanding binary is essential for computer science.

---

11. Common Binary Values

Powers of 2

:

2⁰ = 1
2¹ = 2
2² = 4
2³ = 8
2⁴ = 16
2⁵ = 32
2⁶ = 64
2⁷ = 128

Common Binary to Decimal

:

0 (binary) = 0 (decimal)
1 (binary) = 1 (decimal)
10 (binary) = 2 (decimal)
11 (binary) = 3 (decimal)
100 (binary) = 4 (decimal)
101 (binary) = 5 (decimal)
1111 (binary) = 15 (decimal)
10000 (binary) = 16 (decimal)

---

12. When to Use Each Method

-

pow() Method

: Good for learning - clear and straightforward.

-

Bit Manipulation

: Best for efficiency - avoids pow() overhead, recommended.

-

String Method

: Good for learning string operations - processes left to right.

-

Recursion

: Educational - helps understand recursive thinking.

-

stoi() Method

: Simplest - one line, but requires string conversion.

Best Practice

: Use bit manipulation method for efficiency, or stoi() for simplicity.

---

13. Important Considerations

Valid Binary Digits

:

Binary numbers can only contain 0 and 1.
This program doesn't validate input - assumes valid binary.
Invalid digits (2-9) would produce incorrect results.

Large Binary Numbers

:

long long is used to handle large binary numbers.
Very large binary numbers may still cause overflow.
Consider using string for very large numbers.

Reading Direction

:

Binary is read from right to left (least significant to most significant).
Position 0 is the rightmost digit (ones place).

---

14. return 0;

This ends the program successfully.

---

Summary

Binary-to-decimal conversion multiplies each digit by 2^position and sums results.
The algorithm processes digits from right to left (least significant first).
Modulo (%) extracts digits, division (/) removes digits, pow() calculates position values.
Binary uses base-2 (only 0 and 1), decimal uses base-10 (0-9).
Understanding binary is essential for computer science and digital systems.
Multiple methods exist: pow(), bit manipulation, string, recursion, stoi().
Choose method based on needs: learning vs. efficiency vs. simplicity.

This program is fundamental for beginners learning number systems, understanding how computers represent numbers, and preparing for more advanced topics in computer science and digital systems in C++ programs.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Convert Binary to Decimal

This C++ program is part of the "Advanced Number Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents