C Program to Convert Binary Number to Decimal Number

June 5, 2021

This program converts binary number to equivalent decimal number.

Example: Program to convert binary to decimal

In this program, we have created a user defined function binaryToDecimal() for binary to decimal conversion. This programs takes the binary number (entered by user) as input and converts it into a decimal number using function. To understand this program, you should be familiar with the following C programming concepts:

  1. User defined function in C
  2. C Programming while loop
#include <stdio.h>
#include <math.h>
int binaryToDecimal(long binarynum)
{
    int decimalnum = 0, temp = 0, remainder;
    while (binarynum!=0)
    {
        remainder = binarynum % 10;
        binarynum = binarynum / 10;
        decimalnum = decimalnum + remainder*pow(2,temp);
        temp++;
    }
    return decimalnum;
}

int main()
{
    long binarynum;
    printf("Enter a binary number: ");
    scanf("%ld", &binarynum);

    printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum));
    return 0;
}

Output:

Enter a binary number: 1010111
Equivalent decimal number is: 87


admin