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


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

Output of the above program :

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

Note: ↲ indicates enter is pressed.