C++ Program to Convert Decimal to Binary (Using Bitwise Operator, Array, Function, and Recursion)
Introduction
This article demonstrates how to write a C++ program to convert a decimal number to binary, using various techniques:
-
Bitwise operator
-
Without array
-
With function
-
With array
-
Using recursion
Each version is fully explained with step-by-step code and a clean explanation section.
1. C++ Program to Convert Decimal to Binary Using Bitwise Operator
// C++ program to convert decimal to binary using bitwise operator
#include <iostream>
using namespace std;
// The main function of the program
int main()
{
// Step-1 Declare variables
int num;
// Step-2 Input decimal number
cout << "Enter a decimal number: ";
cin >> num;
// Step-3 Display binary using bitwise operator
cout << "Binary representation: ";
for (int i = 31; i >= 0; i--) {
int bit = (num >> i) & 1;
cout << bit;
}
cout << endl;
return 0;
}
Output
Enter a decimal number: 6
Binary representation: 00000000000000000000000000000110
Explanation of the Code:
-
Declare Variables: We declare an integer variable
num
to store the input. -
Input Value: Take user input using
cin
. -
Bitwise Operation: Use right shift
(>>)
and bitwise AND(& 1)
to extract each bit from left to right (31 to 0). -
Output: Print all bits one after the other to form the binary number.
2. C++ Program to Convert Decimal to Binary Without Using Array
// C++ program to convert decimal to binary without using array
#include <iostream>
using namespace std;
// The main function of the program
int main()
{
// Step-1 Declare variables
int num;
long long binary = 0;
int place = 1;
// Step-2 Input decimal number
cout << "Enter a decimal number: ";
cin >> num;
// Step-3 Convert to binary without using array
while (num != 0) {
int remainder = num % 2;
binary += remainder * place;
num /= 2;
place *= 10;
}
// Step-4 Output the binary number
cout << "Binary representation: " << binary << endl;
return 0;
}
Output
Enter a decimal number: 10
Binary representation: 1010
Explanation of the Code:
-
Declare Variables:
num
,binary
, andplace
are initialized. -
Input Value: Decimal number is input via
cin
. -
Conversion Logic: Using modulus and division, we generate binary digits and build the result by multiplying with place value.
-
Output: Print the final binary number using
cout
.
3. C++ Program to Convert Decimal to Binary Using Function
// C++ program to convert decimal to binary using function
#include <iostream>
using namespace std;
// Function to convert decimal to binary
long long convertToBinary(int num)
{
long long binary = 0;
int place = 1;
// Convert to binary using mathematical logic
while (num != 0) {
int remainder = num % 2;
binary += remainder * place;
num /= 2;
place *= 10;
}
return binary;
}
// The main function of the program
int main()
{
// Step-1 Declare variables
int number;
// Step-2 Input decimal number
cout << "Enter a decimal number: ";
cin >> number;
// Step-3 Call the function to convert
long long result = convertToBinary(number);
// Step-4 Output the result
cout << "Binary representation: " << result << endl;
return 0;
}
Output
Enter a decimal number: 8
Binary representation: 1000
Explanation of the Code:
-
Declare Variables:
number
is the input; result is the returned binary number. -
Function Use:
convertToBinary()
handles conversion logic. -
Separation of Logic: This approach follows good coding practice by separating logic and input/output.
-
Output: Print the result returned by the function.
4. C++ Program to Convert Decimal to Binary Using Array
// C++ program to convert decimal to binary using array
#include <iostream>
using namespace std;
// The main function of the program
int main()
{
// Step-1 Declare variables
int num, binary[32], index = 0;
// Step-2 Input decimal number
cout << "Enter a decimal number: ";
cin >> num;
// Step-3 Convert decimal to binary using array
while (num != 0) {
binary[index++] = num % 2;
num /= 2;
}
// Step-4 Output the binary number in reverse
cout << "Binary representation: ";
for (int i = index - 1; i >= 0; i--) {
cout << binary[i];
}
cout << endl;
return 0;
}
Output
Enter a decimal number: 15
Binary representation: 1111
Explanation of the Code:
-
Declare Array:
binary[32]
stores up to 32 binary digits. -
Conversion Logic: Digits are stored starting from least significant, so output is printed in reverse.
-
Simple Looping: Division by 2 and storing remainders is the core logic.
-
Output: Loop through the array in reverse to print binary digits.
5. C++ Program to Convert Decimal to Binary Using Recursion
// C++ program to convert decimal to binary using recursion
#include <iostream>
using namespace std;
// Recursive function to convert and print binary
void convertToBinary(int num)
{
if (num == 0)
return;
// Recursive call
convertToBinary(num / 2);
// Print remainder
cout << (num % 2);
}
// The main function of the program
int main()
{
// Step-1 Declare variable
int num;
// Step-2 Input decimal number
cout << "Enter a decimal number: ";
cin >> num;
// Step-3 Print binary using recursion
cout << "Binary representation: ";
if (num == 0)
cout << "0";
else
convertToBinary(num);
cout << endl;
return 0;
}
Output
Enter a decimal number: 5
Binary representation: 101
Explanation of the Code:
-
Recursive Approach: Function calls itself with
num / 2
untilnum
becomes0
. -
Print on Return: Digits are printed during function return (unwinding phase).
-
Handles Edge Case: If
num == 0
, output is printed as 0. -
Elegant Solution: This is a clean and concise recursive approach to decimal to binary conversion in C++.
6. C++ Program to Convert Decimal to Binary Using std::bitset
(Header Utility Method)
// C++ program to convert decimal to binary using bitset
#include <iostream>
#include <bitset> // Required for bitset
using namespace std;
// The main function of the program
int main()
{
// Step-1 Declare variable
int num;
// Step-2 Input decimal number
cout << "Enter a decimal number: ";
cin >> num;
// Step-3 Use bitset to convert and print binary
cout << "Binary representation: " << bitset<32>(num) << endl;
return 0;
}
Output
Enter a decimal number: 9
Binary representation: 00000000000000000000000000001001
Explanation of the Code:
-
Use of
bitset
:C++ STL's bitset<32>
is used to directly convert and print binary for a32-bit
integer. -
No Manual Logic Needed: This method is the easiest if the full-length binary is needed.
-
Useful for Fixed Width Representation: Often used in system-level programming.
7. C++ Program to Convert Decimal to Binary Using String Concatenation
// C++ program to convert decimal to binary using string
#include <iostream>
#include <string>
using namespace std;
// The main function of the program
int main()
{
// Step-1 Declare variables
int num;
string binary = "";
// Step-2 Input decimal number
cout << "Enter a decimal number: ";
cin >> num;
// Step-3 Convert using string operations
if (num == 0)
binary = "0";
else {
while (num > 0) {
binary = to_string(num % 2) + binary;
num /= 2;
}
}
// Step-4 Output the result
cout << "Binary representation: " << binary << endl;
return 0;
}
Output
Enter a decimal number: 13
Binary representation: 1101
Explanation of the Code:
-
String Building: Each digit is prepended to the string.
-
Avoids Arrays or Recursion: Uses
std::string
instead. -
Readable and Efficient for Small Inputs: Especially in beginner projects.
8. C++ Program to Convert Decimal to Binary Using Stack (Reverse Order)
// C++ program to convert decimal to binary using stack
#include <iostream>
#include <stack>
using namespace std;
// The main function of the program
int main()
{
// Step-1 Declare variables
int num;
stack<int> binaryStack;
// Step-2 Input decimal number
cout << "Enter a decimal number: ";
cin >> num;
// Step-3 Convert using stack to reverse bits
if (num == 0) {
cout << "Binary representation: 0\n";
return 0;
}
while (num > 0) {
binaryStack.push(num % 2);
num /= 2;
}
// Step-4 Output binary from stack
cout << "Binary representation: ";
while (!binaryStack.empty()) {
cout << binaryStack.top();
binaryStack.pop();
}
cout << endl;
return 0;
}
Output
Enter a decimal number: 20
Binary representation: 10100
Explanation of the Code:
-
Stack for Reversal: Binary digits are pushed in order and popped to print in reverse.
-
Data Structure Use: This approach helps explain how stacks work.
-
Educational Use: Great for beginners learning both stacks and binary conversion.