Convert Binary Number to Decimal and Vice-Versa in C++



In this article, we will learn how to convert a binary number to a decimal number and a decimal number to a binary number using C++. This means converting numbers like 5 or 10 into a format that uses only 0s and 1s, and also converting binary numbers back into decimal.

Decimal number system has a base of 10 as it uses 10 digits from 0 to 9, while binary has a base of 2 as it uses only 0 and 1. Binary is used in computers because digital devices can only represent two states: on(1) and off(0).

Example

Here are some examples of decimal numbers and their corresponding binary numbers:

Decimal Number Binary Number
15 01111
10 01010
18 10010
27 11011

Converting Binary to Decimal

Let's understand how to convert a binary number to a decimal number. The conversion process involves multiplying each bit(0 or 1) by 2 raised to its position(starting from the right) and then adding up all those values. This method follows the binary positional system.

For example, given the binary number 1011:

We calculate it like this:
Input: 1011
=> 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 1 * 2^0 
=> 1*8 + 0*4 + 1*2 + 1*1 
=> 8 + 0 + 2 + 1 = 11
output: 11
So, the decimal result is 11.

Example

Here's a C++ program that converts a binary number to a decimal number using the logic we just discussed.

#include <iostream>
#include <cmath>
using namespace std;

// Function to convert binary to decimal
int binaryToDecimal(long long binary) {
    int decimal = 0, i = 0;
    // Loop through each digit of the binary number
    while (binary != 0) {
        int digit = binary % 10;  // Get the last digit of the binary number
        decimal += digit * pow(2, i);  // Add the corresponding decimal value
        ++i;  // Move to the next power of 2
        binary /= 10;  // Remove the last digit from the binary number
    }
    return decimal;  // Return the decimal result
}

int main() {
    long long binary = 1010;
    int result = binaryToDecimal(binary);
    cout << "Binary number: " << binary << endl;
    cout << "Decimal equivalent: " << result << endl;
    return 0;
}

The output of the above program shows the conversion of a binary number to its decimal form.

Binary number: 1010
Decimal equivalent: 10

Time Complexity: O(log N) because the loop runs based on the number of digits in the binary number.

Space Complexity: O(1) because only a constant amount of space is used.

Converting Decimal to Binary

Next, let's understand how to convert a decimal number to a binary number. To do this, we repeatedly divide the decimal number by 2, noting down the remainder at each step. Then, we reverse the remainders to get the binary equivalent.

For example, given the decimal number 11, we perform the following division:

Input: 11
11 \ 2 = 5 remainder 1  
5 \ 2 = 2 remainder 1  
2 \ 2 = 1 remainder 0  
1 \ 2 = 0 remainder 1
Now, we reverse the remainders: 1011
Output: 1011
So, the binary equivalent of 11 is 1011.

Example

Below you will see the complete C++ program for converting a decimal number to binary by repeatedly dividing by 2 and storing the remainders, which are then reversed to form the binary result.

#include <iostream>
using namespace std;

// Function to convert decimal to binary
void decimalToBinary(int decimal) {
    int binary[32], i = 0;

    // Divide the number by 2 and store the remainder
    while (decimal > 0) {
        binary[i] = decimal % 2;  // Store 0 or 1
        decimal /= 2;             // Reduce the number
        ++i;
    }

    // Show the binary number in correct order
    cout << "Binary equivalent: ";
    for (int j = i - 1; j >= 0; --j) {
        cout << binary[j];
    }
    cout << endl;
}

int main() {
    int decimal = 10; 
    cout << "Decimal number: " << decimal << endl;
    decimalToBinary(decimal);
    return 0;
}

The output below shows the conversion of the decimal number 10 to its binary equivalent.

Decimal number: 10
Binary equivalent: 1010

Time Complexity: O(log N) because the loop runs based on the number of bits in the decimal number.

Space Complexity: O(log N) because the binary array stores the binary digits.

Updated on: 2025-05-09T15:31:37+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements