Write a program that Asks the user to input a nonnegative in

Write a program that: Asks the user to input a non-negative integer value in base 10. Asks the user to input a base to which the base 10 value should be converted. The user should be able to request bases 2, 3, 4, 8, or 16. Uses the Quotient/Remainder method to perform the conversion. No other method is acceptable. Print out the base 10 number and the number in the requested base. Note that for Hex you must obviously use A, B, C, D, E, and F for 10, 11, 12, 13, 14, and 15 respectively Continues this loop until the user enters a-1 as the base 10 value.

Solution

// C code
#include <stdio.h>
#include <string.h>
int main()
{
char baseValues[16] = {\'0\', \'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\', \'A\', \'B\', \'C\', \'D\', \'E\', \'F\'};

while(1)
{

int result[64];
int number, base, i=0;

printf(\"Enter a non-negative number in base 10: \");
scanf(\"%d\",&number);

if(number < 0)
break;

printf(\"Enter the base to convert: \");
scanf(\"%d\",&base);


while (number != 0)
{
result[i] = number % base;
number = number / base;
++i;
}

--i;
printf(\"\ Converted Number = \");
for( ; i>=0; i--)
{
printf(\"%c\", baseValues[result[i]]);
}
printf(\"\ \ \");
}

return 0;
}

/*
output:
Enter a non-negative number in base 10: 15
Enter the base to convert: 2

Converted Number = 1111

Enter a non-negative number in base 10: 1928
Enter the base to convert: 16

Converted Number = 788

Enter a non-negative number in base 10: 776
Enter the base to convert: 8

Converted Number = 1410

Enter a non-negative number in base 10: -1

*/

 Write a program that: Asks the user to input a non-negative integer value in base 10. Asks the user to input a base to which the base 10 value should be conver
 Write a program that: Asks the user to input a non-negative integer value in base 10. Asks the user to input a base to which the base 10 value should be conver

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site