C Program to Convert Decimal to Binary

Introduction

Converting a decimal number to its binary equivalent is a common task in computer science. A binary number is a number expressed in the base-2 numeral system, which uses only two symbols: 0 and 1. To convert a decimal number to binary, you repeatedly divide the number by 2 and record the remainders. The binary equivalent is then formed by reading the remainders in reverse order.

Example:

  • Input: 10 (decimal)
  • Output: 1010 (binary)

Problem Statement

Create a C program that:

  • Takes a decimal number as input.
  • Converts the decimal number to its binary equivalent.
  • Outputs the binary number.

Solution Steps

  1. Include the Standard Libraries: Use #include <stdio.h> for standard input-output functions.
  2. Implement the Decimal to Binary Conversion:
  • Divide the decimal number by 2.
  • Record the remainder (0 or 1).
  • Update the number by dividing it by 2.
  • Repeat until the number becomes 0.
  • Store the remainders and display them in reverse order.
  1. Create a Main Function: Allow the user to input a decimal number and display its binary equivalent.

C Program to Convert Decimal to Binary

#include <stdio.h>

void decimalToBinary(int n) {
    int binaryNum[32];  // Array to store binary number
    int i = 0;

    // Edge case: if n is 0, binary is 0
    if (n == 0) {
        printf("0");
        return;
    }

    // Process to convert decimal to binary
    while (n > 0) {
        binaryNum[i] = n % 2;  // Store remainder in binaryNum array
        n = n / 2;
        i++;
    }

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

int main() {
    int n;

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

    // Function call to convert decimal to binary
    decimalToBinary(n);

    return 0;  // Return 0 to indicate successful execution
}

Explanation

Decimal to Binary Conversion

  • Array binaryNum[32]: This array is used to store the binary digits (bits). The size 32 is chosen to handle typical integer sizes in C (a 32-bit integer).
  • Loop: The loop divides the decimal number n by 2, storing the remainder (either 0 or 1) in the array binaryNum. The loop continues until n becomes 0.
  • Reverse Output: After the loop, the binary digits are stored in reverse order (the least significant bit is at the beginning). The program then prints the array from the last filled index down to the first to get the correct binary representation.

Main Function

  • The main function allows the user to input a decimal number, calls the decimalToBinary function to convert it, and then displays the result.

Output Example

Example Output 1:

Enter a decimal number: 10
Binary representation: 1010

Example Output 2:

Enter a decimal number: 255
Binary representation: 11111111

Example Output 3 (Edge Case):

Enter a decimal number: 0
Binary representation: 0

Conclusion

This C program demonstrates how to convert a decimal number to its binary equivalent. It handles typical cases as well as edge cases, such as when the input is 0. The program provides a simple and effective way to understand and implement the decimal-to-binary conversion process in C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top