C Program to Convert Binary Number to Decimal Number

Question: write a program in C language to read a binary number from a user and convert it to decimal number.

C Source Code: Binary to Decimal Conversion


#include<stdio.h>
#include<conio.h>

int main()
{
	 long int binary, num, decimal=0, base=1, rem;
	 clrscr();
	 printf("Enter valid binary number: ");
	 scanf("%ld", &binary);
	 num = binary;
	 while(num!=0)
	 {
	  rem = num%10;
	  decimal = decimal + base*rem;
	  base = base * 2;
	  num = num/10;
	 }
	 printf("Binary (%ld) = Decimal (%ld)", binary, decimal);
	 getch();
	 return(0);
}

Output of the above program :

Enter valid binary number: 1011010111 ↲
Binary (1011010111) = Decimal (727)  

Note: ↲ indicates enter is pressed.