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

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

Binary numbers are essential in computer science as they form the base of all computations. Converting a binary number (base-2) to a decimal number (base-10) is a fundamental task in programming. In this blog post, we’ll walk you through how to write a C program to perform this conversion.

1. Understanding Binary to Decimal Conversion

A binary number consists of only two digits: 0 and 1. Each digit in a binary number represents a power of 2, starting from the rightmost digit (which represents 20). To convert a binary number to a decimal number, we sum the powers of 2 for the positions where there is a 1.

For example, consider the binary number 1011:

  (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0) = 8 + 0 + 2 + 1 = 11
  

The binary number 1011 converts to the decimal number 11.

2. Writing the Program

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

Program to Convert Binary to Decimal

#include <stdio.h>
#include <math.h>

int convertBinaryToDecimal(long long binary) {
    int decimal = 0, i = 0, remainder;

    // Loop through each digit of the binary number
    while (binary != 0) {
        remainder = binary % 10;  // Get the last digit
        binary = binary / 10;     // Remove the last digit
        decimal += remainder * pow(2, i);  // Convert and add to decimal result
        ++i;
    }

    return decimal;
}

int main() {
    long long binary;

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

    // Convert to decimal and display the result
    int decimal = convertBinaryToDecimal(binary);
    printf("The decimal equivalent is: %d\n", decimal);

    return 0;
}

Explanation of the Code

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

  • The function convertBinaryToDecimal() takes a binary number as input and returns its decimal equivalent.
  • In the while loop, we process each digit of the binary number by extracting the last digit using the modulus operator %, and then removing it by dividing by 10.
  • We calculate the power of 2 for each position and add it to the decimal result.
  • Finally, the main function reads the binary number from the user, calls the conversion function, and prints the decimal result.

3. Example Output

Here’s an example of how the program works:

Enter a binary number: 1011
The decimal equivalent is: 11

4. Common Mistakes to Avoid

  • Input Format: Ensure that the input binary number contains only 0s and 1s. Any other digits will lead to incorrect results.
  • Overflow: The program uses the long long data type to handle larger binary numbers. Be cautious when working with very large binary numbers, as they may exceed the size of standard integer types.

5. Conclusion

In this post, we have written a program to convert a binary number to its decimal equivalent. This basic program helps build a strong foundation in understanding how binary and decimal numbers are related. Once you understand how to convert between different number systems, you can solve more complex problems in digital logic and computer programming.

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post