Convert Decimal to Binary in C++
Decimal to Binary Conversion in C++ (5 Programs).
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}
Output
Enter a decimal number: 10 Decimal: 10 = Binary: 1010
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
This header provides:
1#include <iostream>
Declaring Variables
The program declares:
int decimal;
long long binary = 0;
int remainder, i = 1;
Taking Input From the User
The program asks:
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.
1cout << "Enter a decimal number: ";
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;
}
1return (decimal % 2) + 10 * decimalToBinary(decimal / 2);
Method 3: Using bitset
bitset<32> binary(decimal);
cout << binary.to_string();
1#include <bitset>
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:
Output:
Decimal: 10 = Binary: 1010
This clearly shows both the original decimal number and its binary equivalent.
1cout << "Decimal: " << decimal << " = Binary: " << binary << endl;
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
-
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
:
Leading Zeros
:
Negative Numbers
:
return 0;
This ends the program successfully.