C Program to Convert Binary to Decimal

Introduction

Converting a binary number to its decimal equivalent is a common task in computer science. A binary number is expressed in the base-2 numeral system, using only the digits 0 and 1. To convert a binary number to decimal, you multiply each binary digit by 2 raised to the power of its position (starting from 0 for the rightmost digit) and then sum all the results.

Example:

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

Problem Statement

Create a C program that:

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

Solution Steps

  1. Include the Standard Libraries: Use #include <stdio.h> for standard input-output functions.
  2. Implement the Binary to Decimal Conversion:
  • Read the binary number as an integer or string.
  • Convert each digit starting from the least significant bit (rightmost).
  • Multiply each digit by 2 raised to the power of its position and sum the results.
  1. Create a Main Function: Allow the user to input a binary number and display its decimal equivalent.

C Program to Convert Binary to Decimal

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

int binaryToDecimal(long long n) {
    int decimalNumber = 0, i = 0, remainder;

    while (n != 0) {
        remainder = n % 10;
        n = n / 10;
        decimalNumber += remainder * pow(2, i);
        ++i;
    }

    return decimalNumber;
}

int main() {
    long long n;

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

    // Function call to convert binary to decimal
    int decimal = binaryToDecimal(n);

    // Output the result
    printf("The decimal equivalent is: %d\n", decimal);

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

Explanation

Binary to Decimal Conversion

  • Function binaryToDecimal(long long n): This function converts the binary number n to its decimal equivalent.
    • Remainder: Each digit (bit) of the binary number is extracted by taking n % 10.
    • Power Calculation: Each bit is multiplied by 2 raised to the power of its position i, which starts at 0 and increments with each iteration.
    • Summing Up: The result is added to decimalNumber.
    • The loop continues until all digits of n are processed.

Main Function

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

Output Example

Example Output 1:

Enter a binary number: 1010
The decimal equivalent is: 10

Example Output 2:

Enter a binary number: 1111
The decimal equivalent is: 15

Example Output 3:

Enter a binary number: 100000
The decimal equivalent is: 32

Conclusion

This C program demonstrates how to convert a binary number to its decimal equivalent. It handles typical cases and provides a simple and effective way to understand and implement the binary-to-decimal conversion process in C programming. The use of a loop and the power function allows for the correct conversion of binary digits to their corresponding decimal values.

Leave a Comment

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

Scroll to Top