C Program to Convert Decimal to Binary

Decimal to binary conversion is a common task in C programming, often used in systems where binary representation is crucial, like computing or electronics. This article will demonstrate how to write a C program to convert decimal to binary using three different methods. Each method includes code examples, explanations, and outputs to ensure you fully understand the conversion process.

Let’s explore how to convert decimal numbers to binary and get a step closer to mastering binary conversion in C language. You can also refer to our complete guide to learn how to convert Binary To Decimal.

Three methods to write a “C Program to Convert Decimal to Binary”

Decimal and Binary Number Systems

Before diving into the code, let’s briefly review the decimal and binary number systems:

  • Decimal Number System: The decimal system is the standard system for denoting integer and non-integer numbers. It is a base-10 system, which means it uses ten digits (0-9).
  • Binary Number System: Almost all modern computers and digital systems use the binary system, a base-2 numeral system, internally.It uses only two symbols, typically 0 and 1.

Method 1) C Program to Convert Decimal to Binary Iterative Division by 2

Overview

The first method is the traditional approach of dividing the decimal number by 2 until the quotient becomes 0. You obtain the binary digits as remainders and then reverse them to get the final binary number.

Code Example

#include <stdio.h>

void decimalToBinary(int n) {
    int binaryNum[32];
    int i = 0;
    
    while (n > 0) {
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }
    
    printf("Binary representation: ");
    for (int j = i - 1; j >= 0; j--) {
        printf("%d", binaryNum[j]);
    }
    printf("\n");
}

int main() {
    int n;
    printf("Enter a decimal number: ");
    scanf("%d", &n);
    decimalToBinary(n);
    return 0;
}

Explanation

  1. Division by 2: You divide the decimal number by 2 and store the remainder in an array, continuing this process until the quotient reaches zero.
  2. Reverse Order: You print the remainders in reverse order to generate the binary number.

This is the most straightforward C program to convert decimal to binary and is easy to understand for beginners.

Output

Enter a decimal number: 10
Binary representation: 1010

Method 2) C Program to Convert Decimal to Binary using Recursion

Overview

The second method uses recursion to convert decimal to binary. Recursion is a powerful tool that allows a function to call itself. In this approach, we recursively divide the decimal number by 2 and print the binary digits during the return phase.

Code Example

#include <stdio.h>

void decimalToBinary(int n) {
    if (n > 1) {
        decimalToBinary(n / 2);
    }
    printf("%d", n % 2);
}

int main() {
    int n;
    printf("Enter a decimal number: ");
    scanf("%d", &n);
    printf("Binary representation: ");
    decimalToBinary(n);
    printf("\n");
    return 0;
}

Explanation

  • Recursive calls: You divide the number by 2 in each recursive call until it becomes 1 or 0.
  • Print remainder: After the recursive calls, you print the remainders in sequence.

Recursion simplifies the conversion logic but requires an understanding of how recursive functions work in C programming..

Output

Enter a decimal number: 10
Binary representation: 1010

Method 3) C Program to Convert Decimal to Binary using Bitwise Operations

Overview

Bitwise operations offer a more efficient and low-level approach to convert decimal to binary in C. This method manipulates bits directly, which is faster and more suited for systems programming.

Code Example

#include <stdio.h>

void decimalToBinary(int n) {
    unsigned int mask = 1 << (sizeof(int) * 8 - 1);

    printf("Binary representation: ");
    for (int i = 0; i < sizeof(int) * 8; i++) {
        if (n & mask) {
            printf("1");
        } else {
            printf("0");
        }
        mask >>= 1;
    }
    printf("\n");
}

int main() {
    int n;
    printf("Enter a decimal number: ");
    scanf("%d", &n);
    decimalToBinary(n);
    return 0;
}

Explanation

  • Bitwise AND: This program checks each bit of the integer using a bitwise AND operation.
  • Masking: A mask is used to shift through each bit and print the corresponding 1 or 0.

This method is highly efficient and demonstrates how binary representation is managed at the hardware level.

Output

Enter a decimal number: 10
Binary representation: 00000000000000000000000000001010

10 Most Common FAQs on Decimal to Binary Conversion

1) What is the simplest way to convert decimal to binary in C?

The simplest way to convert decimal to binary in C is by using the iterative division by 2 method.

2) What is the most efficient way to convert decimal to binary in C?

Using bitwise operations is the most efficient method for binary conversion in C as it directly manipulates bits.

3) Can recursion be used for decimal to binary conversion?

Yes, recursion is an elegant method for decimal to binary conversion in C that eliminates the need for arrays.

4) How does bitwise operation work in binary conversion?

Bitwise operations check each bit of the integer by using a mask and printing the corresponding binary digit.

5) Why is binary important in computing?

Binary is the fundamental language of computers, allowing them to process and store data efficiently.

6) Can I convert negative decimal numbers to binary in C?

Yes, but negative numbers are typically represented using two’s complement notation, which requires additional logic.

7) How many bits are required to represent a decimal number in binary?

The number of bits required depends on the size of the decimal number. For example, an 8-bit binary number can represent decimal numbers from 0 to 255.

8) How do I handle large numbers in decimal to binary conversion?

For large numbers, you can use data types like long or unsigned long in C to handle the binary conversion.

9) What is the difference between binary and decimal systems?

The decimal system is base-10, using digits from 0-9, while the binary system is base-2, using only 0 and 1.

10) Is there a built-in function in C to convert decimal to binary?

No, C does not have a built-in function to convert decimal to binary, but you can implement it using the methods discussed.

Conclusion

Converting decimal to binary in C is an essential skill for any programmer working with low-level operations or systems programming. In this article, we’ve explored three methods to achieve this conversion—using division, recursion, and bitwise operations. Each method offers a different level of complexity and efficiency, allowing you to choose the approach that best fits your needs.

By mastering these methods, you can handle binary number conversions in C confidently, whether you’re working on embedded systems, memory manipulation, or algorithm development. Keep experimenting with these examples, and you’ll soon be proficient in performing C language binary conversions.

Happy coding!

Categories C

Leave a Comment