0
0
CppProgramBeginner · 2 min read

C++ Program to Convert Decimal to Binary

You can convert a decimal number to binary in C++ by repeatedly dividing the number by 2 and storing the remainders, then printing them in reverse order; for example, use a loop with number % 2 to get bits and number /= 2 to reduce the number.
📋

Examples

Input5
OutputBinary of 5 is 101
Input10
OutputBinary of 10 is 1010
Input0
OutputBinary of 0 is 0
🧠

How to Think About It

To convert decimal to binary, think of dividing the number by 2 repeatedly and collecting the remainders. These remainders represent the binary digits from least significant to most significant. When the number becomes zero, stop and reverse the collected digits to get the binary form.
📐

Algorithm

1
Get the decimal number as input
2
If the number is zero, print 0 and stop
3
While the number is greater than zero:
4
- Find remainder when divided by 2 (this is a binary digit)
5
- Store the remainder
6
- Divide the number by 2 to reduce it
7
Print the stored remainders in reverse order
💻

Code

cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    int number;
    cout << "Enter a decimal number: ";
    cin >> number;

    if (number == 0) {
        cout << "Binary of 0 is 0" << endl;
        return 0;
    }

    string binary = "";
    int n = number;
    while (n > 0) {
        binary = to_string(n % 2) + binary;
        n /= 2;
    }

    cout << "Binary of " << number << " is " << binary << endl;
    return 0;
}
Output
Enter a decimal number: 10 Binary of 10 is 1010
🔍

Dry Run

Let's trace the input 10 through the code

1

Input number

number = 10

2

Initialize variables

binary = "", n = 10

3

First loop iteration

n % 2 = 0, binary = "0", n = 10 / 2 = 5

4

Second loop iteration

n % 2 = 1, binary = "1" + "0" = "10", n = 5 / 2 = 2

5

Third loop iteration

n % 2 = 0, binary = "0" + "10" = "010", n = 2 / 2 = 1

6

Fourth loop iteration

n % 2 = 1, binary = "1" + "010" = "1010", n = 1 / 2 = 0

7

Loop ends

n = 0, binary = "1010"

8

Print result

Output: Binary of 10 is 1010

n before divisionn % 2binary stringn after division
10005
51102
200101
1110100
💡

Why This Works

Step 1: Divide and get remainder

Each time we divide the number by 2, the remainder (number % 2) gives the next binary digit starting from the right.

Step 2: Build binary string

We add each new remainder to the front of the string to keep the correct order of bits.

Step 3: Stop when zero

When the number becomes zero, all binary digits are collected, so we stop and print the result.

🔄

Alternative Approaches

Using bitwise operators
cpp
#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter a decimal number: ";
    cin >> number;

    if (number == 0) {
        cout << "Binary of 0 is 0" << endl;
        return 0;
    }

    bool started = false;
    cout << "Binary of " << number << " is ";
    for (int i = 31; i >= 0; i--) {
        int bit = (number >> i) & 1;
        if (bit == 1) started = true;
        if (started) cout << bit;
    }
    cout << endl;
    return 0;
}
This method uses bit shifts and prints bits from the highest to lowest, avoiding string concatenation but always checks 32 bits.
Using recursion
cpp
#include <iostream>
using namespace std;

void printBinary(int n) {
    if (n == 0) return;
    printBinary(n / 2);
    cout << n % 2;
}

int main() {
    int number;
    cout << "Enter a decimal number: ";
    cin >> number;

    if (number == 0) {
        cout << "Binary of 0 is 0" << endl;
    } else {
        cout << "Binary of " << number << " is ";
        printBinary(number);
        cout << endl;
    }
    return 0;
}
This recursive approach prints bits in correct order naturally but uses function calls which may be less efficient for large numbers.

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

Time Complexity

The loop runs once for each binary digit, which is proportional to the logarithm base 2 of the number, so O(log n).

Space Complexity

We store the binary digits in a string, which requires space proportional to the number of bits, O(log n).

Which Approach is Fastest?

The bitwise method is fastest because it avoids string operations and uses fixed 32 iterations, but uses more space for output formatting.

ApproachTimeSpaceBest For
Division and remainderO(log n)O(log n)Simple and clear for beginners
Bitwise operatorsO(1) fixed 32 loopsO(1)Performance-critical code
RecursionO(log n)O(log n) stackElegant code, but less efficient
💡
Use string concatenation at the front or recursion to keep binary digits in correct order easily.
⚠️
Beginners often print remainders in the order they are found without reversing, resulting in reversed binary output.