Binary numbers are represented in the base of 2. For example 110112, 1002, etc. In this article, we will be converting a binary number into a decimal number which is represented in base 10 using C language. In the conversion, there are mainly three steps to be followed. The first thing is to get the remainder when the binary number is divided by 10. Next thing to follow is to increment the decimal variable which is to be returned with the product of remainder and the base. Finally, the binary number is divided by 10. All these three procedures happen until the binary number equals to 0. Let’s take a simple example and get it clarified.

Convert Binary to Decimal using C

Binary Number: 101

Decimal Number = 22 * 1 + 21 * 0 + 20 * 1 = 4 + 0 + 1 = 5

Let’s move on to the code to see how this works in C. Like in other languages, we first take the input from the user and use the input for the conversion. An important factor to mention is the data type of the input variable. It is always recommended to use long rather than int as integers can’t hold larger numbers and which can lead to runtime errors.

long binaryToDecimal(long number)
{
  
   long base = 1, remainder, decimalValue = 0;
   //check if the entered number is valid
   while (number > 0)
    {
        // use modulus to get the remainder of the number after dividing it by 10
        remainder = number % 10;
        // Add the (remainder * base) to decimal number
        decimalValue += remainder * base;
        // Divide the number by 10
        number = number / 10 ;
        // Increment base by product of 2
        base = base * 2;
    }
    return decimalValue;
}

Now call the function

#include <stdio.h>
 
void main()
{
    long number;
 
    // prompt the user to enter the binary number
    printf("Please enter your binary number: ");
    scanf("%d", &number); 

    //call the function
    printf("The decimal equivalent to Binary %d is: %d\n",number, binaryToDecimal(number));
}

In the example above, we are using a separate method to do the conversion. Inside the main method, the conversion method is called by passing the input parameter from the user to it. In the conversion method binaryToDecimal(), the first thing is to check whether the input number from the user is equal to zero or not. If it equals to zero, the decimal variable is returned immediately. When the number is not equal to zero, the binary number is divided by 10, and the remainder is saved into a variable.

Next, the decimal variable is incremented with the product of remainder we found earlier and the base which is a multiple of 2. After that, the number is divided by 10 and quotient is saved to the same variable.

Finally, the base is incremented by a factor of 2 and assigned to itself. This whole procedure iterates until the inputted number becomes 0. When it does, the decimal variable which holds the decimal number is returned to the user.

Output

Please enter your binary number: 110011011

The decimal equivalent to Binary 110011011 is: 411

Please enter your binary number: 100001111

The decimal equivalent to Binary 100001111 is: 271

Please enter your binary number: 0

The decimal equivalent to Binary 0 is: 0

Related Articles

Last modified: March 29, 2019