Convert binary to decimal program in C

In this C programming guide we will see some C program to convert a binary number to its equivalent decimal number.

We know that binary number consists of only two digits 0 and 1 where decimal number consists of any number of digits from 0 to 9. In this program we will take the input from user which will be the binary number. Then we will convert it to the equivalent decimal number using C program. You can do this program in different way. Let’s see the program bellow;

If you want to know how to convert a binary number to decimal number mathematically then see this binary to decimal number conversion guide.

C program to convert binary to decimal

// c program to convert binary to decimal

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

int main(){
  int deNum = 0, rem, tempNum = 0;
  long biNum;
  
  printf("Enter the binary number here : ");
  scanf("%ld", &biNum);

  while(biNum != 0){
    rem = biNum % 10;
    biNum /= 10;
    deNum = deNum + rem * pow(2, tempNum);
    tempNum++;
  }
  
  printf("Decimal equivalence is = %d", deNum);
  return 0;
}

Output of this binary to decimal program :

binary-to-decimal-c-program-c-program-to-convert-binary-to-decimal

Convert binary to decimal using function in C

// convert binary to decimal using function in C

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

int convertBtoD(long bNum);

int main(){
  long b;

  printf("Enter any binary number to convert : ");
  scanf("%ld", &b);

  int d = convertBtoD(b);

  printf("\nDecimal equivalence is = %d\n", d);
  return 0;
}

int convertBtoD(long bNum){
  int dNum = 0, tNum = 0, rem;

  while (bNum!=0){
    rem = bNum % 10;
    bNum = bNum / 10;
    dNum = dNum + rem*pow(2,tNum);
    tNum++;
  }

  return dNum;
}

Output of this binary to decimal program

Enter any binary number to convert : 1011001

Decimal equivalence is = 89

Previous pageSum of natural numbers

Next pageDecimal to octal by C