0
0
CppProgramBeginner · 2 min read

C++ Program to Convert Binary to Decimal Number

You can convert binary to decimal in C++ by reading the binary as a string and using a loop to multiply each digit by 2 raised to the power of its position, then summing these values. For example, use a loop to process each character of the binary string and calculate the decimal value.
📋

Examples

Input101
Output5
Input1101
Output13
Input0
Output0
🧠

How to Think About It

To convert binary to decimal, think of the binary number as a series of digits where each digit represents a power of 2. Starting from the rightmost digit (which is the smallest power), multiply each digit by 2 raised to its position index and add all these results together to get the decimal number.
📐

Algorithm

1
Get the binary number as a string input.
2
Initialize a decimal result variable to 0.
3
Loop through each character of the binary string from left to right.
4
For each character, convert it to an integer (0 or 1).
5
Multiply this integer by 2 raised to the power of (length of string - current index - 1).
6
Add the result to the decimal variable.
7
After the loop ends, return or print the decimal result.
💻

Code

cpp
#include <iostream>
#include <string>
#include <cmath>

int main() {
    std::string binary;
    std::cout << "Enter a binary number: ";
    std::cin >> binary;

    int decimal = 0;
    int length = binary.length();

    for (int i = 0; i < length; ++i) {
        if (binary[i] == '1') {
            decimal += static_cast<int>(std::pow(2, length - i - 1));
        }
    }

    std::cout << "Decimal equivalent: " << decimal << std::endl;
    return 0;
}
Output
Enter a binary number: 101 Decimal equivalent: 5
🔍

Dry Run

Let's trace the binary number '101' through the code.

1

Input

binary = '101', length = 3, decimal = 0

2

First iteration (i=0)

binary[0] = '1', decimal += 2^(3-0-1) = 2^2 = 4, decimal = 4

3

Second iteration (i=1)

binary[1] = '0', no addition, decimal = 4

4

Third iteration (i=2)

binary[2] = '1', decimal += 2^(3-2-1) = 2^0 = 1, decimal = 5

5

End

decimal = 5, output printed

IterationCharacterPowerValue AddedDecimal Total
01244
10104
21015
💡

Why This Works

Step 1: Reading Input

The program reads the binary number as a string to process each digit individually.

Step 2: Calculating Decimal Value

Each '1' digit contributes 2 raised to the power of its position index from right to left, summed to get the decimal number.

Step 3: Using pow Function

The std::pow function calculates powers of 2 for each digit's position.

🔄

Alternative Approaches

Using bit shifting
cpp
#include <iostream>
#include <string>

int main() {
    std::string binary;
    std::cout << "Enter a binary number: ";
    std::cin >> binary;

    int decimal = 0;
    for (char bit : binary) {
        decimal = (decimal << 1) + (bit - '0');
    }

    std::cout << "Decimal equivalent: " << decimal << std::endl;
    return 0;
}
This method uses bit shifting to multiply by 2, which is faster and more efficient than using pow.
Using stoi with base 2
cpp
#include <iostream>
#include <string>

int main() {
    std::string binary;
    std::cout << "Enter a binary number: ";
    std::cin >> binary;

    int decimal = std::stoi(binary, nullptr, 2);

    std::cout << "Decimal equivalent: " << decimal << std::endl;
    return 0;
}
This uses the built-in <code>stoi</code> function with base 2 to convert directly, which is very concise but requires C++11 or later.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through each binary digit once, so time grows linearly with input size.

Space Complexity

Only a few variables are used regardless of input size, so space is constant.

Which Approach is Fastest?

Bit shifting is faster than using pow because it uses simple binary operations instead of floating-point math.

ApproachTimeSpaceBest For
Using pow in loopO(n)O(1)Simple understanding, educational
Bit shiftingO(n)O(1)Efficient and fast for large inputs
stoi with base 2O(n)O(1)Concise code, requires C++11+
💡
Use bit shifting to convert binary to decimal efficiently by shifting left and adding bits.
⚠️
Forgetting to convert character digits to integers before calculation causes wrong results.