In programming, converting a decimal number (base 10) to a binary number (base 2) is a common task, especially in low-level programming and digital systems design. In this blog post, we will go over how to write a simple C program to convert a decimal number to its binary equivalent.
Understanding binary numbers is fundamental to programming, as computers store data in binary format (0s and 1s). Converting decimal to binary is often the first step when working with bit manipulation, memory storage, or low-level hardware interactions.
What is Decimal and Binary?
Before diving into the code, let’s briefly discuss decimal and binary numbers.
- Decimal (Base 10): This is the number system we use in daily life. It has 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
- Binary (Base 2): This number system uses only two digits: 0 and 1. Every decimal number can be represented in binary, which is crucial for digital electronics.
For example:
- The decimal number 13 is represented as 1101 in binary.
- The decimal number 5 is represented as 101 in binary.
Algorithm to Convert Decimal to Binary
To convert a decimal number to binary, we can use the following method:
- Divide the decimal number by 2 and record the remainder.
- Store the remainders.
- Continue dividing the quotient by 2 until the quotient is 0.
- The binary equivalent is the sequence of remainders read in reverse order.
C Program to Convert Decimal to Binary
Now that we have the algorithm, let’s write a C program to perform the decimal to binary conversion.
#include <stdio.h>
void decimalToBinary(int n) {
// Array to store binary number
int binary[32];
int i = 0;
// Edge case: if the number is 0, print 0
if (n == 0) {
printf("Binary: 0\n");
return;
}
// Divide the number by 2 and store the remainders
while (n > 0) {
binary[i] = n % 2; // Store remainder
n = n / 2; // Update the number
i++;
}
// Print the binary number in reverse order
printf("Binary: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}
int main() {
int decimal;
// Input: Decimal number
printf("Enter a decimal number: ");
scanf("%d", &decimal);
// Function call to convert decimal to binary
decimalToBinary(decimal);
return 0;
}
Explanation of the Code:
- Input and Output:
- The program prompts the user to input a decimal number.
- It then calls the
decimalToBinary()
function to convert this decimal number into binary and prints the result.
- Function
decimalToBinary()
:- This function takes an integer
n
(the decimal number) as input. - We use an array
binary[32]
to store the binary digits. The array is large enough to handle 32-bit integers. - A
while
loop is used to divide the number by 2 repeatedly. Each time, the remainder (either 0 or 1) is stored in the array. - After all the divisions are done, the remainders are printed in reverse order to get the correct binary representation.
- This function takes an integer
- Edge Case:
- If the input is 0, the program directly prints
0
as the binary output.
- If the input is 0, the program directly prints
Sample Output
Example 1:
Enter a decimal number: 13
Binary: 1101
Example 2:
Enter a decimal number: 5
Binary: 101
How the Program Works (Step-by-Step):
- When the user inputs
13
:- First division: 13 ÷ 2 = 6 remainder 1
- Second division: 6 ÷ 2 = 3 remainder 0
- Third division: 3 ÷ 2 = 1 remainder 1
- Fourth division: 1 ÷ 2 = 0 remainder 1
- The remainders are [1, 0, 1, 1], so the binary equivalent is
1101
.
- When the user inputs
5
:- First division: 5 ÷ 2 = 2 remainder 1
- Second division: 2 ÷ 2 = 1 remainder 0
- Third division: 1 ÷ 2 = 0 remainder 1
- The remainders are [1, 0, 1], so the binary equivalent is
101
.
Important Notes:
- Binary Representation: The program works with 32-bit integers, meaning it can handle positive decimal numbers up to 2^31 – 1 (i.e., 2,147,483,647). If you want to handle larger numbers, consider using
long long
or other data types with more bits. - Negative Numbers: The current program does not handle negative numbers. You can extend the program to handle negative numbers by converting them to their 2’s complement representation if needed.
Conclusion
Converting a decimal number to binary is a fundamental concept in computer science and digital electronics. In this blog post, we demonstrated how to write a simple C program to perform the conversion using basic division and remainder techniques.
This program serves as a foundation for more advanced tasks, such as working with bitwise operations, digital circuits, and data encoding. By understanding and implementing this conversion, you’ll gain valuable insights into how computers interpret and store numerical data in binary form.
Feel free to experiment with the code, modify it for different use cases, and further enhance its functionality!