Decimal numbers are the standard number system we use daily, based on base 10. Computers, however, work with binary (base 2) numbers. Converting decimal numbers into binary is a fundamental operation in programming, helping us understand how values are stored and processed internally. In this article, we will write C programs to convert decimal numbers into binary format.
Understanding The Problem
A decimal number can be converted to binary by dividing it repeatedly by 2
and recording the remainders. The binary number is then constructed by reading these remainders in reverse order. For example, the decimal number 10
becomes binary 1010
. Our task is to take a decimal integer from the user and print its binary equivalent.
Program 1: Using Loops and Arrays
The simplest way is to divide the decimal number by 2
repeatedly and store remainders in an array. Finally, the array is printed in reverse order.
#include <stdio.h>
int main() {
int n, binary[32], i = 0;
printf("Enter a decimal number: ");
scanf("%d", &n);
while (n > 0) {
binary[i++] = n % 2;
n /= 2;
}
printf("Binary equivalent: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
return 0;
}
This program collects remainders while dividing by 2 and prints them in reverse. It is simple and works on all standard C compilers.
Program 2: Using Recursion
Recursion provides an elegant way to display binary digits in the correct order without using arrays.
#include <stdio.h>
void decimalToBinary(int n) {
if (n == 0)
return;
decimalToBinary(n / 2);
printf("%d", n % 2);
}
int main() {
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
if (n == 0) {
printf("0");
} else {
decimalToBinary(n);
}
printf("\n");
return 0;
}
This program recursively divides the number until it reaches zero, printing remainders on the way back. It avoids arrays and offers a concise implementation.
Program 3: Using %b
Format Specifier (C23)
C23 introduces the %b
format specifier to print integers as binary. This method is concise and avoids manual calculations.
#include <stdio.h>
int main() {
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
// %b prints binary in C23; removes leading zeros
printf("Binary equivalent: %b\n", n);
return 0;
}
This program directly prints the binary representation of a decimal number. It is extremely simple and efficient but requires a C23-compliant compiler. For older compilers, the loop or recursion methods remain necessary.
FAQs
Q1: Which method is more efficient, loops or recursion?
Loops are generally more efficient, but recursion offers a cleaner and shorter implementation.
Q2: Can this program handle large numbers?
Yes, but you may need larger arrays or long long
type for very large inputs in the loop method.
Q3: What about negative numbers?
The programs above do not directly handle negative numbers. Extra logic is required to represent them in binary using two’s complement.
Conclusion
Converting decimal to binary is one of the most fundamental concepts in computer science. Iterative, recursive, and C23 %b
approaches provide simple and effective solutions. Understanding these methods helps you handle binary conversions efficiently in various programming scenarios.
References & Additional Resources
- C Programming Tutorial – Covers C basics with examples on operators and loops.
- GeeksforGeeks – Decimal to Binary – Explains multiple approaches for decimal-to-binary conversion.
- TutorialsPoint – Recursion in C – Great reference for learning recursive techniques.