Convert a Decimal Number to Binary using Loops in C
To convert a decimal number to binary in C, we repeatedly divide the number by 2 and store the remainders. By reversing the collected remainders, we get the binary representation. This process is commonly implemented using loops.
Examples of Decimal to Binary Conversion
1. Convert Decimal to Binary Using a While Loop
In this example, we will take a decimal number as input, use a while
loop to repeatedly divide it by 2, store the remainders, and display the binary equivalent.
main.c
#include <stdio.h>
int main() {
int decimal, binary[32], index = 0;
// Input from user
printf("Enter a decimal number: ");
scanf("%d", &decimal);
// Convert decimal to binary
while (decimal > 0) {
binary[index] = decimal % 2; // Store remainder
decimal = decimal / 2; // Update decimal value
index++;
}
// Print binary number in reverse order
printf("Binary representation: ");
for (int i = index - 1; i >= 0; i--) {
printf("%d", binary[i]);
}
return 0;
}
Explanation:
- We declare an integer array
binary[32]
to store the binary digits. - User input is taken using
scanf()
and stored indecimal
. - A
while
loop is used to dividedecimal
by 2, storing the remainder in the array. - The loop continues until
decimal
becomes 0. - A
for
loop prints the binary digits in reverse order (from the last remainder to the first).
Output:
Enter a decimal number: 10
Binary representation: 1010
2. Convert Decimal to Binary Using a For Loop
In this example, we will use a for
loop instead of a while
loop to achieve the conversion.
main.c
#include <stdio.h>
int main() {
int decimal, binary[32], index;
// Input from user
printf("Enter a decimal number: ");
scanf("%d", &decimal);
// Convert decimal to binary using for loop
for (index = 0; decimal > 0; index++) {
binary[index] = decimal % 2;
decimal = decimal / 2;
}
// Print binary number in reverse order
printf("Binary representation: ");
for (int i = index - 1; i >= 0; i--) {
printf("%d", binary[i]);
}
return 0;
}
Explanation:
- We declare an integer array
binary[32]
to store the remainders. - User input is stored in the variable
decimal
. - A
for
loop is used to calculate the binary representation, updatingdecimal
until it reaches 0. - Binary digits are stored in
binary[]
and later printed in reverse order.
Output:
Enter a decimal number: 7
Binary representation: 111
3. Convert Decimal to Binary Using Bitwise Operators
In this example, we will use bitwise operators to convert a decimal number to binary efficiently.
main.c
#include <stdio.h>
void printBinary(int num) {
for (int i = 31; i >= 0; i--) {
int bit = (num >> i) & 1; // Extract each bit using bitwise shift and AND
printf("%d", bit);
}
}
int main() {
int decimal;
// Input from user
printf("Enter a decimal number: ");
scanf("%d", &decimal);
// Display binary using bitwise operations
printf("Binary representation: ");
printBinary(decimal);
return 0;
}
Explanation:
- The function
printBinary()
prints the binary representation of an integer. - We iterate from the highest bit position (31) to 0.
- Each bit is extracted using bitwise right shift (
num >> i
) and bitwise AND (& 1
). - User input is read into
decimal
, andprintBinary()
is called to display the result.
Output:
Enter a decimal number: 5
Binary representation: 00000000000000000000000000000101
Conclusion
In this tutorial, we explored multiple ways to convert a decimal number to binary in C:
- Using a
while
loop to store remainders. - Using a
for
loop to track conversions. - Using bitwise operators for direct binary extraction.
Each method has its own advantages depending on efficiency and clarity of implementation.