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.
1#include <iostream>2using namespace std;34int main() {5 int decimal;6 long long binary = 0;7 int remainder, i = 1;89 cout << "Enter a decimal number: ";10 cin >> decimal;1112 int temp = decimal;1314 while (temp != 0) {15 remainder = temp % 2;16 temp /= 2;17 binary += remainder * i;18 i *= 10;19 }2021 cout << "Decimal: " << decimal << " = Binary: " << binary << endl;2223 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
Read 10.
- 2
10 % 2 = 0, temp = 5; add 0 × 1.
- 3
5 % 2 = 1, temp = 2; add 1 × 10.
- 4
2 % 2 = 0, temp = 1; add 0 × 100.
- 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:
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:
Header File Used
#include <iostream>
This header provides:
Declaring Variables
The program declares:
int decimal;
long long binary = 0;
int remainder, i = 1;
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:
Step 2:
Step 3:
Step 4:
Step 5:
Understanding the Conversion Process
Why Divide by 2?:
Why Read Remainders in Reverse?:
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):
Building Process:
Why Multiply i by 10?:
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);
}
Method 3: Using bitset
#include <bitset>
bitset<32> binary(decimal);
cout << binary.to_string();
Method 4: Using String
string binary = "";
while (decimal > 0) {
binary = to_string(decimal % 2) + binary;
decimal /= 2;
}
Method 5: Using Bit Manipulation
string binary = "";
for (int i = 31; i >= 0; i--) {
binary += ((decimal >> i) & 1) ? '1' : '0';
}
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:
When Does It Stop?:
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:
Pattern Recognition:
When to Use Each Method
Best Practice: Use while loop for learning, bitset for simplicity, or bit manipulation for efficiency.
Important Considerations
Integer Overflow:
Leading Zeros:
Negative Numbers:
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.