#include<stdio.h>
main()
{
int n,z,r,b,b1,b2,d,d1,o,o1,h,i,j,y1[10]={0},y2[10]={0},y3[10]={0},y4[10]={0};
char a,x[20];
printf("\tConversions\n1.Binary to Decimal\n2.Decimal to Binary\n3.Binary to Octal\n4.Octal to Binary\n5.Binary to Hexadecimal\n6.Hexadecimal to Binary\n7.Exit");
while(1)
{
printf("\nEnter you choice :");
scanf("%d",&n);
switch(n)
{
case 1:
printf("Enter the Binary number\n");
scanf("%d",&b);
z=1,d=0;
while(b>0)
{
r=b%10;
if(r>1)
{
printf("Invalid Entry");
exit(0);
}
d=d+(r*z);
z=z*2;
b=b/10;
}
printf("Decimal equivalent = %d",d);
break;
case 2:
printf("Enter the Decimal number\n");
scanf("%d",&d1);
j=0;
while(d1>0)
{
r=d1%2;
y1[j]=r;
j++;
d1=d1/2;
}
printf("Binary equivalent =");
for(i=j-1;i>=0;i--)
printf("%d",y1[i]);
break;
case 3:
printf("Enter the Binary number\n");
scanf("%d",&b1);
z=1,o=0,j=0;
while(b1>0)
{
r=b1%2;
if(r>1)
{
printf("Invalid entry");
exit(0);
}
o=o+(r*z);
z=z*2;
b1=b1/10;
}
while(o>0)
{
r=o%8;
y2[j]=r;
j++;
o=o/8;
}
printf("Octal equivalent = ");
for(i=j-1;i>=0;i--)
printf("%d",y2[i]);
break;
case 4:
printf("Enter the Octal number\n");
scanf("%d",&o1);
z=1,b=0,j=0;
while(o1>0)
{
r=o1%10;
if(r>7)
{
printf("Invalid entry");
exit(0);
}
b=b+(r*z);
z=z*8;
o1=o1/10;
}
while(b>0)
{
r=b%2;
y3[j]=r;
j++;
b=b/2;
}
printf("Binary equivalent = ");
for(i=j-1;i>=0;i--)
printf("%d",y3[i]);
break;
case 5:
printf("Enter the Binary number\n");
scanf("%d",&b2);
z=1,h=0,j=0;
while(b2>0)
{
r=b2%2;
if(r>1)
{
printf("Invalid entry");
exit(0);
}
h=h+(r*z);
z=z*2;
b2=b2/10;
}
while(h>0)
{
r=h%16;
y4[j]=r;
j++;
h=h/16;
}
printf("Hexadecimal equivalent = ");
for(i=j-1;i>=0;i--)
{
switch(y4[i])
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default:
printf("%d",y4[i]);
break;
}
}
break;
case 6:
printf("Enter the Hexadecimal number\n");
scanf("%s",x);
printf("Binary equivalent = ");
for(i=0;x[i]!='\0';i++)
{
a=x[i];
switch(a)
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'A':
printf("1010");
break;
case 'B':
printf("1011");
break;
case 'C':
printf("1100");
break;
case 'D':
printf("1101");
break;
case 'E':
printf("1110");
break;
case 'F':
printf("1111");
break;
default:
printf("Invalid entry");
break;
}
}
break;
case 7:
exit(0);
}
}
}