Coding Assignment (20 points in total) Write a C program that can convert a deci
ID: 3752856 • Letter: C
Question
Coding Assignment (20 points in total) Write a C program that can convert a decimal into other number bases. The program first asks for a decimal integer ("Enter a decimal integer:In"). The program then printout the decimal integer in base 2,8,16 format. The program will continue to ask for a binary number ("Enter a binary integer:n). The program print out the decimal, octal, and hex value for the binary integer Exam Enter a decimal integer: 17 The number in binary is: 0b10001 The number in octal is: 0021 The number in hex is: 0x11 Enter a binary integer: 10010 The number in decimal is: 18 The number in octal is: 0022 The number in hex is: 0x12 Note: You need to pad a prefix(0b,00,0x to binary, octal, hex) when you print the value. For each output line, add a at the beginning. Test cases: The testcases are stored can be downloaded via the fol cp /home/rwang67/public/lab3/ Is command: What to submit: 1. Save your program as number.c 2. Compile your program with gcc and test your program with your own testcase 3. Test your program with all given test cases. The following command will test with the case 1 and save the ouput to number input1> output1 4. Zip the c program and output files into a tar file: username lab3.tar.gz tar -czvf username_lab3.tar.gz file1 file2.... the file to the submission public folder: 5. cp username lab3.tar.gz /home/rwang67/public/submissions 6. Upload a screenshot of your terminal running any one of the test cases.Explanation / Answer
Program to convert Decimal to all other bases and Binary to all other bases
#include <stdio.h>
long decimalToBinary(long n);
int decimalToOctal(int n);
void Dec2Hex(int n);
int binaryToDecimal(int n);
int convertBinarytoOctal(int binaryNumber);
int convertBinarytoHexa(int binaryNumber);
int main() {
long decimal;
printf("Enter a decimal integer: ");
scanf("%ld", &decimal);
printf("*The number in binary is: 0b%ld",decimalToBinary(decimal));
printf(" *The number in octal is 0o%d ",decimalToOctal(decimal));
printf("*The number in hex is: 0x");
Dec2Hex(decimal);
int binary;
printf(" Enter a binary number ");
scanf("%d", &binary);
printf("*The number in decimal is %d ",binaryToDecimal(binary));
convertBinarytoOctal(binary);
convertBinarytoHexa(binary);
return 0;
}
/* Function to convert a Decimal number to Binary number */
long decimalToBinary(long n) {
int remainder;
long binary = 0, i = 1;
while(n != 0) {
remainder = n%2;
n = n/2;
binary= binary + (remainder*i);
i = i*10;
}
return binary;
}
/* Function to convert a Decimal number to Octal number */
int decimalToOctal(int decimalNumber)
{
int octalNumber = 0, i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
/* Function to convert a Decimal number to Hexa number */
void Dec2Hex(int no){
int hex=0;
if(!no)
return;
else {
hex=no%16;
Dec2Hex(no/16);
}
if(hex>9)
printf("%c",'A'+(hex-10));
else
printf("%d",hex);
}
/* Function to convert a Binary number to Decimal number */
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp)
{
int last_digit = temp % 10;
temp = temp/10;
dec_value += last_digit*base;
base = base*2;
}
return dec_value;
}
/* Function to convert a Binary number to Octal number */
int convertBinarytoOctal(int binarynum)
{
long int octalnum = 0, j = 1, remainder;
while (binarynum != 0)
{
remainder = binarynum % 10;
octalnum = octalnum + remainder * j;
j = j * 2;
binarynum = binarynum / 10;
}
printf("*The number in octal is 0o%lo ", octalnum);
return 0;
}
/* Function to convert a Binary number to Hexa number */
int convertBinarytoHexa(int binaryval)
{
long int hexadecimalval = 0, i = 1, remainder;
while (binaryval != 0)
{
remainder = binaryval % 10;
hexadecimalval = hexadecimalval + remainder * i;
i = i * 2;
binaryval = binaryval / 10;
}
printf("*The number in octal is 0x%lX ", hexadecimalval);
return 0;
}
Thank you,
WIth regards.