C Program to Convert Decimal to Binary (Using Bitwise Operator, Array, Function, and Recursion)
Introduction
This article presents various ways to write a C program to convert decimal to binary, covering five popular approaches:
-
Using bitwise operator
-
Without using array
-
Using function
-
Using array
-
Using recursion
These examples help in understanding binary number conversion and common programming techniques such as loops, functions, bitwise operations, and recursion.
All the programs follow a clean, beginner-friendly, and well-commented C programming structure.
1. Write a C Program to Convert Decimal to Binary Number System Using Bitwise Operator
// C program to convert decimal to binary using bitwise operator
#include <stdio.h>
// The main function of the program
int main()
{
// Step-1 Declare variables
int num, i;
// Step-2 Input a decimal number from the user
printf("Enter a decimal number: ");
scanf("%d", &num);
// Step-3 Display binary representation using bitwise operations
printf("Binary representation: ");
for(i = 31; i >= 0; i--) {
// Extract i-th bit from the left
int bit = (num >> i) & 1;
printf("%d", bit);
}
printf("\n");
return 0;
}
Output
Enter a decimal number: 5
Binary representation: 00000000000000000000000000000101
Explanation
In this program:
-
We input a decimal number using
scanf()
. -
We use a
for
loop that iterates from bit position31
down to0
(total 32 bits). -
Inside the loop:
-
(num >> i) & 1
extracts the i-th bit of the number.
-
-
This allows us to directly print each bit from the most significant to least significant bit.
-
This technique works efficiently for displaying a full 32-bit binary number.
2. C Program to Convert Decimal to Binary Without Using Array
// C program to convert decimal to binary without using array
#include <stdio.h>
// The main function of the program
int main()
{
// Step-1 Declare variables
int num;
long long binary = 0;
int remainder, place = 1;
// Step-2 Input a decimal number from the user
printf("Enter a decimal number: ");
scanf("%d", &num);
// Step-3 Convert decimal to binary manually using loop
while(num != 0) {
remainder = num % 2;
binary += remainder * place;
num /= 2;
place *= 10;
}
// Step-4 Output the binary number
printf("Binary representation: %lld\n", binary);
return 0;
}
Output
Enter a decimal number: 10
Binary representation: 1010
Explanation
In this version:
-
We declare a
long long
variable to store the binary result. -
Instead of using arrays or strings, we build the binary number using integer math.
-
Each binary digit is calculated using
num % 2
, and added at the correct place value using multiplication. -
The
place
variable starts at 1 and increases tenfold each time to simulate binary positions in decimal digits. -
This version is simple and doesn't use any array, hence it’s memory-efficient for small numbers.
3. C Program to Convert Decimal to Binary Using Function
// C program to convert decimal to binary using a function
#include <stdio.h>
// Step-1 Function to convert decimal to binary
long long convertToBinary(int num)
{
long long binary = 0;
int place = 1;
// Convert decimal to binary using loop
while(num != 0) {
binary += (num % 2) * place;
num /= 2;
place *= 10;
}
return binary;
}
// The main function of the program
int main()
{
// Step-2 Declare variables
int num;
long long binary;
// Step-3 Input a decimal number from the user
printf("Enter a decimal number: ");
scanf("%d", &num);
// Step-4 Call the function and store the result
binary = convertToBinary(num);
// Step-5 Output the result
printf("Binary representation: %lld\n", binary);
return 0;
}
Output
Enter a decimal number: 13
Binary representation: 1101
Explanation
Here:
-
We define a separate function called
convertToBinary()
that performs the conversion. -
The logic inside the function is identical to the non-array version:
-
It calculates the binary number and returns it.
-
-
The main function handles input and output, while the conversion logic is modular.
-
This approach is great for reusability and clean code separation.
4. C Program to Convert Decimal to Binary Using Array
// C program to convert decimal to binary using array
#include <stdio.h>
// The main function of the program
int main()
{
// Step-1 Declare variables
int num, binary[32], i = 0;
// Step-2 Input a decimal number from the user
printf("Enter a decimal number: ");
scanf("%d", &num);
// Step-3 Convert decimal to binary and store in array
while(num > 0) {
binary[i] = num % 2;
num /= 2;
i++;
}
// Step-4 Print the binary number in reverse order
printf("Binary representation: ");
for(int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
return 0;
}
Explanation
In this method:
-
We use an integer array
binary[]
to store binary digits (0s and 1s). -
The conversion happens by taking
num % 2
and storing the remainder in the array. -
After conversion, we print the array in reverse order since the least significant digit is stored first.
-
This approach is ideal for learning how data can be stored and reversed using arrays.
5. Write a C Program to Convert Decimal to Binary Using Recursion
// C program to convert decimal to binary using recursion
#include <stdio.h>
// Step-1 Recursive function to convert and print binary
void convertToBinary(int num)
{
if(num == 0)
return;
// Recursive call
convertToBinary(num / 2);
// Print remainder
printf("%d", num % 2);
}
// The main function of the program
int main()
{
// Step-2 Declare variable
int num;
// Step-3 Input a decimal number from the user
printf("Enter a decimal number: ");
scanf("%d", &num);
// Step-4 Output binary using recursive function
printf("Binary representation: ");
if(num == 0)
printf("0");
else
convertToBinary(num);
printf("\n");
return 0;
}
Explanation
This program:
-
Defines a recursive function that divides the number by
2
until it becomes0
. -
During the recursive unwinding phase, it prints the remainder
num % 2
. -
This results in the binary number being printed in the correct order.
-
It’s a classic recursion problem that also demonstrates how function calls stack up and resolve.
Conclusion
In this article, we explored five different methods to convert a decimal number to binary in C, covering various approaches like:
-
Bitwise operator
-
Without using array
-
Using function
-
Using array
-
Using recursion