Saturday, 10 December 2011

Write a program to print Binary to Decimal numbers in c

#include
#include
main()
{
long int num;
long int rem,dig,sum=0,i,x=1;
clrscr();
printf("Enter a Binary Number:");
scanf("%ld",&num);
for(i=num;i>0;i=i/10)
{
rem=i%10;
dig=rem*x;
sum=sum+dig;
x=x*2;
}
printf("\nBinary Number is %ld\n\nDecimal Number is %ld",num,sum);
getch();
}

OUTPUT
Enter a Binary Number :1111
Binary Number is 1111
Decimal Number is 15

No comments:

Post a Comment