Write a C program called convert2base.c that is similar to convert2hex.c, but ha
ID: 3886823 • Letter: W
Question
Write a C program called convert2base.c that is similar to convert2hex.c, but has 2 command line parameters, the first being an integer in the range 2 though 36, and the second being a decimal number. Instead of converting to base 16, the base used is given by the first parameter. Example If I wrote the program, convert2base 23 314156 would generate the following output: 314156 = 13658 * 23 + 22 (M) 13658 = 593 * 234 19 (J) 593 = 25 * 23 + 18 (1) 25 = 1 * 23 + 2 (2) 1 = 0 * 23 + 1 (1) 12IJM Run your program with each of the following command line parameters and include the output. 23 314156 2 2147483647 31 263429442133726086 36 3881091294471582038Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
int getalpha (int n)
{
// if number less than 10 print number else print alphabet
if (n < 10)
{
printf ("%d", n);
}
else
{
// print the ascii value staring from A
printf ("%c", n + 55);
}
}
int main (int argc, char **argv)
{
// convert char pointer to integer numbers
int b = atoi (argv[1]);
long n;
sscanf(argv[2],"%ld",&n);
printf("%ld ",n);
while (n > 0)
{
// get alpha from the remainder
getalpha (n % b);
//divide the number by 23 for next iteration
n = n / 23;
}
return 0;
}
/*
sample output
23 314156 -> 121JM
2 214783647 -> 1100111
31 2634294421337260886 -> JFJHP9N7CB85R5
36 3881091294471582038 -> 21I0SDH291E5X7
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.