Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are required to write a C program that accepts two decimal integers, say d a

ID: 3543285 • Letter: Y

Question

You are required to write a C program that accepts two decimal integers, say d and r. You may assume that the frst decimal integer d is nonnegative and that the second decimal integer r which represents the radix, will be one of 2, 3, 4, : : :, 15, 16. The program should convert the rst decimal integer d into its representation in radix r and print the result. Examples: (i) Suppose the first decimal integer is 138 and the second decimal integer is 16. In this case, the output produced by your program should be 8A, which is the hexadecimal (radix 16) representation of the decimal integer 138. (ii) Suppose the first decimal integer is 284 and the second decimal integer is 13. In this case, the output produced by your program should be 18B, which is the radix 13 representation of the decimal integer 284. (In base 13, the digits used are 0, 1, 2, : : :, 9, A, B and C, where A, B and C represent 10, 11 and 12 respectively.) Your program should be written so that it handles just one pair of integers. Thus, the outline for your program is as follows. (Note that no error checks are needed.) 1. Prompt the user to type two decimal integers. 2. Read the two integers 3. Convert the rst integer into its representation in the radix specifed by the second integer. 4. Print the representation and stop. Your program must read the two integers from stdin and write the answer to stdout. You may assume that when prompted, the user will type two integers separated by one or more spaces. Note: Recall that for any radix r 2, the digits to be used are 0, 1, : : :, r ??1. Use the letters A, B, C, D, E and F to represent 10, 11, 12, 13, 14 and 15 respectively, as done in the hexadecimal system. Thus, representations in radix 11 can use the digits 0, 1, : : :, 9, A; representations in radix 12 can use 0, 1, : : :, 9, A, B, and so on.

Explanation / Answer

//Please rate the answer

//Ask if you have any doubts


#include <stdio.h>


char checkNumber(int number)

{

switch(number)

{

case 0 : return '0';

case 1 : return '1';

case 2 : return '2';

case 3 : return '3';

case 4 : return '4';

case 5 : return '5';

case 6 : return '6';

case 7 : return '7';

case 8 : return '8';

case 9 : return '9';

case 10 : return 'A';

case 11 : return 'B';

case 12 : return 'C';

case 13 : return 'D';

case 14 : return 'E';

case 15 : return 'F';

}

}

int main()

{

int d,r,i;

printf("Enter two numbers ");

scanf("%d %d",&d,&r);

char output[50];

int index=0;

do

{

output[index] = checkNumber(d%r);

d/=r;

index++;

}while(d!=0);


printf("Number in base %d is: ",r);

for( i = index-1; i>=0 ;i--)

{

printf("%c",output[i] );

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote