Please helpppppppppp In this assignment, you will write a program that converts
ID: 3535721 • Letter: P
Question
Please helpppppppppp
In this assignment, you will write a program that converts number bases. In particular, the program will perform the following tasks:
1) The program should accept entries in decimal, binary, and octal
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
2) The program will convert numbers entered in decimal, binary and octal to other bases
· As practice, users should start by enter values in decimal and convert them to binary, octal and hexadecimal numbers
3) The program should be able to convert to other bases, other than bases 2, 8, and 16
· Restrict the additional bases to 3, 4, 5, 6, 7, and 9.
4) The program must work in reverse as well; e.g. if you can convert a number from decimal to binary, you should also be able to convert from binary to decimal.
Explanation / Answer
C program
#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(" Conversions 1.Binary to Decimal 2.Decimal to Binary 3.Binary to Octal 4.Octal to Binary 5.Binary to Hexadecimal 6.Hexadecimal to Binary 7.Exit");
while(1)
{
printf(" Enter you choice :");
scanf("%d",&n);
switch(n)
{
case 1:
printf("Enter the Binary number ");
scanf("%d",&b);
z=1,d=0;
while(b>0)
{
r=b;
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 ");
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 ");
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 ");
scanf("%d",&o1);
z=1,b=0,j=0;
while(o1>0)
{
r=o1;
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 ");
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;
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 ");
scanf("%s",x);
printf("Binary equivalent = ");
for(i=0;x[i]!='';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);
}
}
}