Program in C to Convert Binary to Decimal

Program in C to Convert Binary to Decimal

Binary numbers are the foundation of digital systems, where data is represented using only two digits: 0 and 1. Decimal numbers, on the other hand, are the numbers we use in everyday life. Converting binary to decimal is essential for understanding how computers interpret and display data. In this article, we will write a program in C to convert binary numbers into decimal form.

Understanding The Problem

A binary number can be converted to decimal by multiplying each digit with powers of 2, starting from the rightmost bit (Least Significant Bit). For example, binary 1011 is calculated as 1*2^3 + 0*2^2 + 1*2^1 + 1*2^0 = 11. Our task is to take a binary number as input and produce its decimal equivalent.

Program 1: Using Arithmetic Method

In this method, the binary number is treated as an integer. We process each digit by taking remainders with 10, multiplying by powers of 2, and accumulating the result.

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

int main() {

    long long binary;
    int decimal = 0, base = 1, rem;

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

    while (binary > 0) {

        rem = binary % 10;
        decimal = decimal + rem * base;
        binary = binary / 10;
        base = base * 2;

    }

    printf("Decimal equivalent: %d\n", decimal);

    return 0;

}

This program divides the binary number digit by digit, multiplies each digit with increasing powers of two, and sums them to obtain the decimal value.

Program 2: Using Strings

Another way is to input the binary number as a string and process it from left to right. This avoids integer overflow for large binary numbers.

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

int main() {

    char binary[65];
    int decimal = 0, len, i;

    printf("Enter a binary number: ");
    scanf("%s", binary);

    len = strlen(binary);

    for (i = 0; i < len; i++) {

        if (binary[i] == '1') {
            decimal += pow(2, len - i - 1);
        }

    }

    printf("Decimal equivalent: %d\n", decimal);

    return 0;

}

Here, each character of the binary string is checked. If it is 1, the corresponding power of two is added to the decimal result. This is efficient and safer for long binary inputs.

Program 3: Using sprintf() and Manual Conversion

This method uses sprintf() and character arithmetic to build the decimal number from a binary string. It is useful if you need the decimal result as a string for further processing.

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

int main() {

    char binary[65];
    int decimal = 0, len, i;
    char decimalStr[20];

    printf("Enter a binary number: ");
    scanf("%s", binary);

    len = strlen(binary);

    for (i = 0; i < len; i++) {

        if (binary[i] == '1') {
            decimal += (int)pow(2, len - i - 1);
        }

    }

    sprintf(decimalStr, "%d", decimal);

    printf("Decimal equivalent: %s\n", decimalStr);

    return 0;

}

This approach converts each binary digit into its corresponding decimal value and then stores the result as a string using sprintf(). It allows easy manipulation or storage of the decimal number as text.

Program 4: Using strtol()

strtol() can safely convert a string representing a number in any base to a decimal integer. Here we use base 2 for binary.

#include <stdio.h>
#include <stdlib.h>

int main() {

    char binaryStr[65];
    long decimal;

    printf("Enter a binary number: ");
    scanf("%s", binaryStr);

    // Convert binary string to decimal using strtol
    decimal = strtol(binaryStr, NULL, 2);  // Base 2 for binary

    printf("Decimal equivalent: %ld\n", decimal);

    return 0;

}

This method is very safe and works well for large binary numbers. It avoids overflow issues and allows storing results in long or long long if needed.

FAQs

Q1: What happens if I enter a binary number with digits other than 0 or 1?
The program may give incorrect results. Proper validation should be added to check input.

Q2: Can this program handle very large binary numbers?
The string-based method and sprintf() version are safer because they avoid integer overflow for long inputs.

Q3: Which method is faster?
The arithmetic method is slightly faster for small numbers, while the string and sprintf() methods are more flexible for large numbers.

Conclusion

Converting binary to decimal is a fundamental concept in computer science. Using arithmetic operations, string processing, or sprintf() allows efficient and safe conversion. Mastering these techniques helps understand data representation and low-level computations in programming.

References & Additional Resources

Scroll to Top