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

</>
Copy
#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:

  1. We declare an integer array binary[32] to store the binary digits.
  2. User input is taken using scanf() and stored in decimal.
  3. A while loop is used to divide decimal by 2, storing the remainder in the array.
  4. The loop continues until decimal becomes 0.
  5. 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

</>
Copy
#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:

  1. We declare an integer array binary[32] to store the remainders.
  2. User input is stored in the variable decimal.
  3. A for loop is used to calculate the binary representation, updating decimal until it reaches 0.
  4. 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

</>
Copy
#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:

  1. The function printBinary() prints the binary representation of an integer.
  2. We iterate from the highest bit position (31) to 0.
  3. Each bit is extracted using bitwise right shift (num >> i) and bitwise AND (& 1).
  4. User input is read into decimal, and printBinary() 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:

  1. Using a while loop to store remainders.
  2. Using a for loop to track conversions.
  3. Using bitwise operators for direct binary extraction.

Each method has its own advantages depending on efficiency and clarity of implementation.