Write a C program that can convert a decimal into other number bases The program
ID: 3752777 • Letter: W
Question
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:n") 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:ln" The program print out the decimal, octal, and hex value for the binary integer. Example: Enter a decimal integer: 17 *The number in octal is: 0o21 *The number in hex is: 0x11 Enter a binary integer: 10010 *The number in decimal is: 18 *The number in octal is: 0o22 *The number in hex is: 0x12 Note: You need to pad a prefix(Ob,00,0x to binary, octal, hex) when you print the value. For each output line, add a *at the beginningExplanation / 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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.