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

Write a program that converts numbers from one base to another. The program prom

ID: 3621198 • Letter: W

Question

Write a program that converts numbers from one base to another. The program prompts the user for three nonnegative integers n, b1, and b2. If n is a valid number in base b1, it prints its representation in base b2, otherwise it prints an appropriate warning. You can assume that b1 and b2 will always be between 2 and 10, inclusive. You cannot use the library. Sample runs follow, recall that n b represents n in base b. For example 101 2 = 5 10.

(~)$ a.out
Value and two bases: 21 10 2
21 10 = 10101 2
(~)$ a.out
Value and two bases: 21012 3 5
21012 3 = 1234 5
(~)$ a.out
Value and two bases: 21012 2 5
21012 is not a valid number in base 2

Explanation / Answer

please rate - thanks

#include <stdio.h>
#include <conio.h>
int from10(int,int);
int to10(int,int);
int checkdigits(int,int);
int main()
{int base,from,number,num;
printf("Value and two bases: ");
scanf("%d %d %d",&number,&from,&base);
num=checkdigits(number,from);
if(num==1)
   {printf("%d is not a valid number in base %d ",number,from);
   getch();
   return 0;
}
else if(from<2||from>10||base<2||base>10)
   {printf("Invalid base program aborted ");
   getch();
return 0;
}
else  
    {
    if(from==10)
          num=from10(number,base);
    else if(base==10)
          num=to10(number,from);
    else
        {num=to10(number,from);
         num=from10(num,base);
        }
        
printf("%d %d = %d %d ",number,from,num,base);  
}
getch();
return 0;
}
int checkdigits(int n,int b)
{int d;
while(n!=0)
{d=n%10;
if(d>=b)
     return 1;
n/=10;
}
return 0;
}
int to10(int a,int n)
{int d,i,j,k,num=0;
i=0;
j=1;
while(a!=0)
    {d=a %10;
    num=num+d*j;
    j=j*n;
    a/=10;
     }
return num;
}
int from10(int number,int base)
{
int i,num=0,n=1;
     while (number>0)                        //do until you run out of digits
       {i=(number % base)*n;
         num=num+i;     //get a digit and change to new base
         number=number/base;
         n=n*10;
        }
return num;
}

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