This C program converts a given decimal number into its binary equivalent. A decimal number uses base 10 (digits 0–9), while a binary number uses base 2 (only 0 and 1) — the language computers actually work in. Converting between them is one of the first things every programmer learns. In this tutorial we walk through the algorithm step by step, give you clean working code, and show a faster bitwise alternative.
The Algorithm
To convert a decimal number to binary, repeatedly divide the number by 2 and record the remainder each time:
- Divide the number by 2 and store the remainder (0 or 1).
- Replace the number with the quotient.
- Repeat until the number becomes 0.
- The binary equivalent is the remainders read in reverse order (bottom to top).
For example, for 13: 13/2 → r1, 6/2 → r0, 3/2 → r1, 1/2 → r1. Reading bottom-up gives 1101.
The Program
#include <stdio.h>
int main(void)
{
int num, original;
int binary[32];
int count = 0;
printf("Enter a decimal number : ");
if (scanf("%d", &num) != 1 || num < 0) {
printf("Please enter a non-negative integer.\n");
return 1;
}
original = num;
/* Handle 0 explicitly so we still print a digit. */
if (num == 0) {
binary[count++] = 0;
}
/* Collect remainders. */
while (num > 0) {
binary[count] = num % 2;
num = num / 2;
count++;
}
/* Print the remainders in reverse order. */
printf("The binary equivalent of %d is : ", original);
for (int i = count - 1; i >= 0; i--)
printf("%d", binary[i]);
printf("\n");
return 0;
}
How the Program Works
- We read the decimal number with
scanfand reject negative input so the loop is well defined. num % 2gives the current binary digit (the remainder), andnum / 2moves to the next position. Each digit is stored in thebinary[]array.- The remainders come out least-significant first, so we print the array backwards with the final
forloop to get the correct binary string. - The
if (num == 0)guard makes sure the program prints0instead of nothing when the user enters zero.
Sample Output
Enter a decimal number : 13 The binary equivalent of 13 is : 1101 Enter a decimal number : 100 The binary equivalent of 100 is : 1100100
Faster Alternative: Bitwise Operators
Because computers store integers in binary already, you can read the bits directly with the right-shift (>>) and bitwise-AND (&) operators instead of dividing:
for (int i = 31; i >= 0; i--)
printf("%d", (num >> i) & 1);
This prints all 32 bits (including leading zeros). It is faster because shifting and masking are single CPU instructions, while integer division is more expensive.
Time Complexity
| Method | Time Complexity | Notes |
|---|---|---|
| Division by 2 | O(log₂ n) | One step per binary digit |
| Bitwise shift | O(1) per bit, fixed 32 iterations | Prints leading zeros |
For a thorough grounding in number systems, bitwise operators, and how C represents integers, The C Programming Language by Kernighan and Ritchie is the classic reference — find it on Amazon.
This post contains affiliate links. If you buy through them, we may earn a small commission at no extra cost to you.
Related C Programs
- C Program to Convert Binary to Decimal
- C Program to Demonstrate the goto Statement
- Complete List of C Programs
Want to try this without installing anything? Paste the code into one of the best online C compilers and run it instantly. For a permanent setup, see our guide to a complete C development environment.