Write a Program to Convert a Decimal Number to a Binary Number in C

Write a Program to Convert a Decimal Number to a Binary Number in C

Decimal numbers are commonly used in everyday life, but in the world of computers, binary numbers play a vital role. Converting a decimal number (base-10) to a binary number (base-2) is a fundamental task in programming. In this blog post, we’ll explore how to write a C program to perform this conversion.

1. Understanding Decimal to Binary Conversion

A decimal number is composed of digits ranging from 0 to 9, and each digit represents a power of 10. Conversely, a binary number consists of only two digits: 0 and 1. Each digit in a binary number represents a power of 2.

To convert a decimal number to binary, we repeatedly divide the number by 2 and keep track of the remainders. The binary representation is obtained by reading the remainders in reverse order.

For example, to convert the decimal number 13 to binary:

  13 / 2 = 6, remainder = 1
  6 / 2 = 3, remainder = 0
  3 / 2 = 1, remainder = 1
  1 / 2 = 0, remainder = 1
  

Reading the remainders from bottom to top gives us the binary representation 1101.

2. Writing the Program

Below is the C program to convert a decimal number to a binary number:

Program to Convert Decimal to Binary

#include <stdio.h>

void convertDecimalToBinary(int decimal) {
    int binary[32];  // Array to store binary number
    int index = 0;   // Index for binary array

    // If decimal is 0, simply print 0
    if (decimal == 0) {
        printf("Binary equivalent: 0\n");
        return;
    }

    // Convert decimal to binary
    while (decimal > 0) {
        binary[index] = decimal % 2;  // Store the remainder
        decimal = decimal / 2;         // Update decimal
        index++;
    }

    // Print binary number in reverse order
    printf("Binary equivalent: ");
    for (int i = index - 1; i >= 0; i--) {
        printf("%d", binary[i]);
    }
    printf("\n");
}

int main() {
    int decimal;

    // Input the decimal number
    printf("Enter a decimal number: ");
    scanf("%d", &decimal);

    // Convert to binary and display the result
    convertDecimalToBinary(decimal);

    return 0;
}

Explanation of the Code

Let’s break down the program step-by-step:

  • The function convertDecimalToBinary() takes a decimal number as input and prints its binary equivalent.
  • We store the binary digits in an array called binary and use a loop to fill this array with the remainders obtained from the division by 2.
  • After obtaining the binary digits, we print them in reverse order since the first remainder corresponds to the least significant bit.
  • The main function reads the decimal number from the user, calls the conversion function, and displays the binary result.

3. Example Output

Here’s an example of how the program works:

Enter a decimal number: 13
Binary equivalent: 1101

4. Common Mistakes to Avoid

  • Negative Numbers: The program currently does not handle negative decimal numbers. Consider adding validation for this case.
  • Input Format: Ensure that the input is a valid integer. Any non-integer input will lead to undefined behavior.

5. Conclusion

In this post, we have written a program to convert a decimal number to its binary equivalent. Understanding how to convert between number systems is essential for various applications in computer science and programming. Once you master this concept, you will find it easier to work with data representation and manipulation in more complex scenarios.

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post