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

Assignment : For this assignment you’ll be designing a program which can take th

ID: 3835015 • Letter: A

Question

Assignment: For this assignment you’ll be designing a program which can take the input of a decimal number and a numerical base, and convert the decimal number to that base. For example, if given the decimal number seven and the base two, your program should output it as 111, which is how seven is represented in binary.

You’ll need to perform these operations on the following combinations:

A: 15, base 2.

B: 38, base 16.

C: 54, base 6.

D: 19, base 8.

E: 27, base 3.

(100 pts total: 50 points for code, 10 points for each output)

Explanation / Answer

#include <stdio.h>
#include <string.h>

char reVal(int num)
{
if (num >= 0 && num <= 9)
return (char)(num + '0');
else
return (char)(num - 10 + 'A');
}

void strev(char *str)
{
int len = strlen(str);
int i;
for (i = 0; i < len/2; i++)
{
char temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
}

char* fromDeci(char res[], int base, int inputNum)
{
int index = 0;
  
while (inputNum > 0)
{
res[index++] = reVal(inputNum % base);
inputNum /= base;
}
res[index] = '';

  
strev(res);

return res;
}

int main()
{
int inputNum, base;
   printf("Enter the decimal number: ");
   scanf("%d", &inputNum);
   printf("Enter the base to convert: ");
   scanf("%d", &base);
char res[100];
printf("Equivalent of %d in base %d is "
" %s ", inputNum, base, fromDeci(res, base, inputNum));
return 0;
}

A:
Enter the decimal number: 15                                                                                                                                                    
Enter the base to convert: 2                                                                                                                                                    
Equivalent of 15 in base 2 is  1111

B:


Enter the decimal number: 38                                                                                                                                                    
Enter the base to convert: 16                                                                                                                                                   
Equivalent of 38 in base 16 is  26

C:


Enter the decimal number: 54                                                                                                                                                    
Enter the base to convert: 6                                                                                                                                                    
Equivalent of 54 in base 6 is  130

D:


Enter the decimal number: 19                                                                                                                                                    
Enter the base to convert: 8                                                                                                                                                    
Equivalent of 19 in base 8 is  23

E:


Enter the decimal number: 27                                                                                                                                                    
Enter the base to convert: 3                                                                                                                                                    
Equivalent of 27 in base 3 is  1000

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