There has been a lot of confusion for Binary to decimal conversion and vice versa. Today morning i came up with some simple self understandable code !!!
Binary to Decimal
#include <stdio.h>
#include <math.h>
#include <graphics.h>
main()
{
int dec;
int total;
int power;
clrscr();
total=0;
power=1;
printf("Enter binary number :\n");
scanf("%d",&dec);
while(dec>0)
{
total += dec%10*power;
dec=dec/10;
power=power*2;
}
printf("%d is the decimal number",total);
}
Decimal to binary
#include <stdio.h>
#include <graphics.h>
main()
{
int decimal=0,rem=0;
clrscr();
printf("Enter Decimal : ");
scanf("%d",&decimal);
while(decimal>0)
{
rem=decimal%2;
decimal=decimal/2;
printf("%d",rem);
}
}