#include <stdio.h>
#include <conio.h>
#include <math.h>
void btod(); /*binary to decimal*/
void dtob(); /*decimal to binary*/
void main()
{
AGAIN: int n;
printf("Convert binary to decimal, Press 1\n");
printf("Convert decimal to binary, Press 2\n");
scanf("%d",&n);
switch(n)
{
case 1:
btod();
break;
case 2:
dtob();
break;
default:
printf("Invalid INPUT");
goto AGAIN;
}
getch();
}
void btod()
{
int b,d=0,r,i=0;
printf("Enter binary number: ");
scanf("%d",&b);
while(b>0)
{
r=b%10;
d+=r*pow(2,i);
b=b/10;
i++;
}
printf("Equivalent Decimal value is %d",d);
}
void dtob()
{
int d,b=0,r,i=1;
printf("Enter decimal number: ");
scanf("%d",&d);
while(d!=0)
{
r=d%2;
b=b+r*i;
d=d/2;
i=i*10;
}
printf("Equivalent Binary value is %d",b);
}
OUTPUT:
No comments:
Post a Comment