Convert a binary number to decimal number in C


Levels of difficulty: / perform operation:

C program to convert a binary number to its equivalent decimal number.

C Program

#include <stdio.h>
#include <conio.h>
void main()
{
	unsigned long num;
	int digit,i,pos=0,pow=1,dec=0;
	clrscr();
	printf("\nENTER A BINARY NUMBER: ");
	scanf("%lu",&num);
	printf("\nDECIMAL EQUIVALANT OF %lu IS ",num);
	while(num!=0)
	{
		pow=1;
		digit=num%10;
		num=num/10;
		for(i=1;i<=pos;i++)
			pow=pow*2;
		pos++;
		dec=dec+(pow*digit);
	}
	printf("%d",dec);
	getch();
}


Output







Quantitative Aptitude
Reasoning
Programming
Interview