Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Convert Decimal to Binary in C++

Convert a decimal integer to binary in C++ by dividing by 2 and collecting remainders. Shows how remainders stack into a long long and where that runs out.

IntermediateAdvanced Number ProgramsExample 6 of 13
convert-decimal-to-binary.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int decimal;
6 long long binary = 0;
7 int remainder, i = 1;
8
9 cout << "Enter a decimal number: ";
10 cin >> decimal;
11
12 int temp = decimal;
13
14 while (temp != 0) {
15 remainder = temp % 2;
16 temp /= 2;
17 binary += remainder * i;
18 i *= 10;
19 }
20
21 cout << "Decimal: " << decimal << " = Binary: " << binary << endl;
22
23 return 0;
24}

Sample Output

Decimal: 10 = Binary: 1010

Remainders 0,1,0,1 placed right to left.

In this one

  • How repeated division by 2 produces binary digits
  • Why remainders appear lowest bit first
  • How multiplying by 10 places each bit as a decimal digit
  • When to use bitset or a string instead

How it works

  1. 1

    Read 10.

  2. 2

    10 % 2 = 0, temp = 5; add 0 × 1.

  3. 3

    5 % 2 = 1, temp = 2; add 1 × 10.

  4. 4

    2 % 2 = 0, temp = 1; add 0 × 100.

  5. 5

    1 % 2 = 1, temp = 0; add 1 × 1000 → 1010.

Edge cases

Input 0

Enter 0.

The loop is skipped and binary stays 0, which is correct.

Large input

Enter 1000000.

The binary form has 20 digits, which no longer fits in long long, and i itself overflows int. Use bitset<32> or build a string.

What's going on

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

Input decimal: 10
Output binary: 1010

The conversion involves repeatedly dividing the decimal number by 2 and collecting the remainders. The binary number is formed by reading the remainders in reverse order (from last to first).

Example:

10 (decimal) → divide by 2 repeatedly → remainders: 0, 1, 0, 1 → reverse → 1010 (binary)
15 (decimal) → remainders: 1, 1, 1, 1 → reverse → 1111 (binary)

Header File Used

#include <iostream>

This header provides:

cout for displaying output
cin for taking input from the user

Declaring Variables

The program declares:

int decimal;

long long binary = 0;

int remainder, i = 1;

decimal stores the decimal number entered by the user.
binary stores the converted binary result (initialized to 0, long long for large results).
remainder stores the remainder from each division by 2.
i is a multiplier that represents the current position (1, 10, 100, 1000, ...).

Taking Input From the User

The program asks:

cout << "Enter a decimal number: ";

cin >> decimal;

The user enters a decimal number, for example: 10

We also create a temporary copy:

int temp = decimal;

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

The Conversion Algorithm

The core conversion uses a while loop:

while (temp != 0) {

remainder = temp % 2;

temp /= 2;

binary += remainder * i;

i *= 10;

}

Let's break down how this works step-by-step for decimal = 10:

Step 1:

remainder = 10 % 2 = 0 (remainder when dividing by 2)
temp = 10 / 2 = 5 (quotient after dividing by 2)
binary = 0 + 0 × 1 = 0
i = 1 × 10 = 10 (move to next position)

Step 2:

remainder = 5 % 2 = 1
temp = 5 / 2 = 2
binary = 0 + 1 × 10 = 10
i = 10 × 10 = 100

Step 3:

remainder = 2 % 2 = 0
temp = 2 / 2 = 1
binary = 10 + 0 × 100 = 10
i = 100 × 10 = 1000

Step 4:

remainder = 1 % 2 = 1
temp = 1 / 2 = 0
binary = 10 + 1 × 1000 = 1010
i = 1000 × 10 = 10000

Step 5:

temp is now 0, so the loop stops
Final result: binary = 1010

Understanding the Conversion Process

Why Divide by 2?:

Binary is base-2, so we divide by 2 repeatedly.
Each division extracts one binary digit (0 or 1) as the remainder.
The quotient becomes the new number to process.

Why Read Remainders in Reverse?:

The first remainder is the least significant bit (rightmost).
The last remainder is the most significant bit (leftmost).
We build the binary number by placing remainders from right to left.

Visual Example (for decimal = 10):

Step 1: 10 ÷ 2 = 5 remainder 0 → binary digit: 0

Step 2: 5 ÷ 2 = 2 remainder 1 → binary digit: 1

Step 3: 2 ÷ 2 = 1 remainder 0 → binary digit: 0

Step 4: 1 ÷ 2 = 0 remainder 1 → binary digit: 1

Reading remainders from bottom to top: 1, 0, 1, 0

Result: 1010 (binary)

How the Binary Number is Built

Position Multiplier (i):

i starts at 1 (ones place).
Each iteration, i is multiplied by 10 (moves to next position).
i values: 1, 10, 100, 1000, 10000, ...

Building Process:

remainder * i places the digit at the correct position.
Example: remainder = 1, i = 1000 → adds 1000 to binary (places 1 at thousands place).
Each new remainder is placed to the left of previous digits.

Why Multiply i by 10?:

We're building a number where each position is 10 times the previous.
This creates: ones, tens, hundreds, thousands, ...
Matches how we write numbers (right to left: ones, tens, hundreds, ...).

Other Methods (Mentioned but not shown in code)

Method 2: Using Recursion

long long decimalToBinary(int decimal) {

if (decimal == 0) return 0;

return (decimal % 2) + 10 * decimalToBinary(decimal / 2);

}

Recursive approach - function calls itself.
Base case: when decimal becomes 0, return 0.
Recursive case: get remainder, recurse on quotient, then combine.

Method 3: Using bitset

#include <bitset>

bitset<32> binary(decimal);

cout << binary.to_string();

bitset is a C++ class for handling binary numbers.
Automatically converts decimal to binary.
Can specify number of bits (e.g., 32 bits).

Method 4: Using String

string binary = "";

while (decimal > 0) {

binary = to_string(decimal % 2) + binary;

decimal /= 2;

}

Builds binary as a string.
Prepends each remainder to the string.
More flexible for very large numbers.

Method 5: Using Bit Manipulation

string binary = "";

for (int i = 31; i >= 0; i--) {

binary += ((decimal >> i) & 1) ? '1' : '0';

}

Uses bitwise right shift (>>) and bitwise AND (&).
Extracts each bit from the number.
More advanced, works at the bit level.

Displaying the Result

The program prints:

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

Output:

Decimal: 10 = Binary: 1010

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

Understanding the Division Process

Repeated Division by 2:

Each division by 2 extracts one binary digit.
The remainder (0 or 1) is the binary digit.
The quotient is used for the next iteration.

When Does It Stop?:

The process stops when the quotient becomes 0.
At this point, all binary digits have been extracted.

Example Trace (for decimal = 10):

Step 1: 10 ÷ 2 = 5 remainder 0 → binary digit: 0

Step 2: 5 ÷ 2 = 2 remainder 1 → binary digit: 1

Step 3: 2 ÷ 2 = 1 remainder 0 → binary digit: 0

Step 4: 1 ÷ 2 = 0 remainder 1 → binary digit: 1

Step 5: 0 (stop)

Reading remainders from last to first: 1, 0, 1, 0 → 1010

Common Decimal to Binary Conversions

Small Numbers:

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

Pattern Recognition:

Powers of 2 have single 1 followed by zeros: 2=10, 4=100, 8=1000, 16=10000
Numbers one less than power of 2 are all 1s: 3=11, 7=111, 15=1111

When to Use Each Method

While Loop: Best for learning - clear and straightforward, shows the algorithm.
Recursion: Educational - helps understand recursive thinking.
bitset: Best for simplicity - one line, handles conversion automatically.
String: Good for very large numbers - more flexible.
Bit Manipulation: Most efficient - works at hardware level, advanced.

Best Practice: Use while loop for learning, bitset for simplicity, or bit manipulation for efficiency.

Important Considerations

Integer Overflow:

Binary representation can be very long.
long long is used to handle larger binary numbers.
For very large decimals, consider using string method.

Leading Zeros:

This method doesn't produce leading zeros.
Example: 5 (decimal) = 101 (binary), not 00000101.
If you need fixed width, use bitset or pad with zeros.

Negative Numbers:

This implementation doesn't handle negative numbers.
For negative numbers, use two's complement representation.
Or convert absolute value and add sign bit.

return 0;

This ends the program successfully.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Binary of 6?

Pick an answer to check it.

Question 2

What does i *= 10 do?

Pick an answer to check it.

Questions people ask

Is there a built-in?

bitset<8>(10) prints 00001010 with #include <bitset>.

How about negative numbers?

This loop is for non-negative values. Computers store negatives in two's complement, which bitset shows.

Why is binary a long long?

Each bit becomes a decimal digit, so the number grows fast.