C++ Program to Convert Decimal to Binary
number % 2 to get bits and number /= 2 to reduce the number.Examples
How to Think About It
Algorithm
Code
#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; }
Dry Run
Let's trace the input 10 through the code
Input number
number = 10
Initialize variables
binary = "", n = 10
First loop iteration
n % 2 = 0, binary = "0", n = 10 / 2 = 5
Second loop iteration
n % 2 = 1, binary = "1" + "0" = "10", n = 5 / 2 = 2
Third loop iteration
n % 2 = 0, binary = "0" + "10" = "010", n = 2 / 2 = 1
Fourth loop iteration
n % 2 = 1, binary = "1" + "010" = "1010", n = 1 / 2 = 0
Loop ends
n = 0, binary = "1010"
Print result
Output: Binary of 10 is 1010
| n before division | n % 2 | binary string | n after division |
|---|---|---|---|
| 10 | 0 | 0 | 5 |
| 5 | 1 | 10 | 2 |
| 2 | 0 | 010 | 1 |
| 1 | 1 | 1010 | 0 |
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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Division and remainder | O(log n) | O(log n) | Simple and clear for beginners |
| Bitwise operators | O(1) fixed 32 loops | O(1) | Performance-critical code |
| Recursion | O(log n) | O(log n) stack | Elegant code, but less efficient |