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

write a c program that converts an integer into another base. the input integer

ID: 638695 • Letter: W

Question

write a c program that converts an integer into another base. the input integer could be in decimal,octal,or hexadecimal. the output could be converted into either of base. a sample run is blow. the user's respone in again in boldface.

integer conversion program

Base of input (d=decimal, h=hexadecimal, o=octal) : d

Number :178

Base of output(d=decimal, h=hexadecimal, o=octal) : h

The integer 178 in decimal is equivalent to b2 in hexadecimal.

Another number? (Y/N) Y

Base of output(d=decimal, h=hexadecimal, o=octal) : o

Number: 423

Base of output(d=decimal, h=hexadecimal, o=octal) : d

The integer 423 in octal is equivalent to 275 in decimal.

Another Number (Y/N) N

Goodbye!

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

char letters(int r); // prototypes function for converting 10's to A's and so on, base 11+ will work

int main() {

     

    int base;

    int d; //declares decimal integer

    int d_clone; // clones d for a loop

    int r; //declares remainder integer

    int *number; //pointer variable to make an array based on how long the converted number is

    int count = 0; //these three are counters

    int i = 0;

    int j = 0;

     

    printf("-- Decimal To Any Base Up to 36 Converter 0.1b -- ");

    printf(" Enter a decimal integer to convert: ");

    scanf("%d", &d);

    printf(" ");

    printf("Enter the base you want it converted to: ");

    scanf("%d", &base);

    printf(" ");

     

    d_clone = d; //copies the decimal number for a loop

     

    if(d < 0) {

         printf("Cannot convert negative numbers.");

    } //end if

     

    if(base==0 | base==1) {

         printf("Cannot have 0 or 1 as a base.");

     } //end if

     

    else {

     

         do {

             r = d_clone % base; //finds remainder everytime the number is divided by the base

             d_clone = d_clone / base; //divides the original number by the base

              

             if(r >= 10) { // if the remainder is 10+ it will convert to letters

                  r = (char) r;

                  r = letters(r);

                  printf("%d R %c ", d_clone, r);

                  count++;

             } // end if

              

             else {

             r = (int) r;

             printf("%d R %d ", d_clone, r);

             count++; //counts how many times it is divisible, represents how long the number is

             } //end else

         } //end while

         while(d_clone != 0); //stops when the number is 0

          

         number = malloc(count * sizeof(int)); //allocates memory for the converted numbers digits

          

         printf(" ");

          

         do {

             r = d % base;

             d = d / base;

             number[i] = r; //sets the digits of the converted number in an array

             i++;

              

         }

         while(i<count); //executes loop until number[0] through number[i] are filled

          

         j = count - 1; // accounts for the 0 index in the array

          

         printf("The number in base %d is: ", base);

          

         for(j = count - 1; j >= 0; j--) {

               if(number[j] >= 10) {

                    number[j] = letters(number[j]);

                    printf("%c", number[j]);

               } //end if

                   

               else {

                    printf("%d", number[j]); //prints the converted number

                             

               } //end else

         } //end for

          

         free(number); //frees the allocated memory

          

    } //end else

     

    getch();

     

    return 0;

}

char letters(int r) {

      

     if(r==10){

               return 'A';   

               }

     if(r==11){

               return 'B';

               }

     if(r==12){

               return 'C';

               }

     if(r==13){

               return 'D';

               }

     if(r==14){

               return 'E';

               }

     if(r==15){

               return 'F';

               }

     if(r==16){

               return 'G';

               }

     if(r==17){

               return 'H';

               }

     if(r==18){

               return 'I';

               }

     if(r==19){

               return 'J';

               }

     if(r==20){

               return 'K';

               }

     if(r==21){

               return 'L';

               }

     if(r==22){

               return 'M';

               }

     if(r==23){

               return 'N';

               }

     if(r==24){

               return 'O';

               }

     if(r==25){

               return 'P';

               }

     if(r==26){

               return 'Q';

               }

     if(r==27){

               return 'R';

               }

     if(r==28){

               return 'S';

               }

     if(r==29){

               return 'T';

               }

     if(r==30){

               return 'U';

               }

     if(r==31){

               return 'V';

               }

     if(r==32){

               return 'W';

               }

     if(r==33){

               return 'X';

               }

     if(r==34){

               return 'Y';

               }

     if(r==35){

               return 'Z';

               }

}