C Programming

Solved Example

Practice Questions

Exercise Question

C Program to find decimal equivalent of a binary number

Objective

Write a program to convert a binary number to an equivalent decimal number.

Program / Solution

#include<stdio.h>
#include<math.h>
int main() {
	int dec,dig,i;
	long bin,old;
	
	printf("Enter any binary number: ");
	scanf("%ld",&bin);
	
	old = bin; dec = 0; i = 0;
	while(bin != 0) {
		dig = bin % 10;
		dec = dec + dig * pow(2,i);	
		bin = bin / 10;
		i++;
	}
	
	
	printf("Decimal equivalent of %ld = %d",old,dec);
	return 0;
}

Sample Output

Enter any binary number: 110010
Decimal equivalent of 110010 = 50