In: Computer Science
Please complete in only C++, using loops
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. Another example, 8,943 in base 10, is 13,236 in base 9.
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.
Hi,
The standard number system range starts from 2 to 16 base and 10,11,12,13,14,15 are represented as A, B, C, D, E, F.
And in the question, nothing is mentioned about standard so i have attached both standard and non-standard code and you can follow any code.
--------------------------------------------------------------------
CODE 1: For given code ,The standard valid base are between 2 to 16 including 2 and 16 and valid values of 10,11,12,13,14,15 are A, B, C, D, E, F respectively.
#include<iostream>
using namespace std;
int main()
{
int base,n,temp,count=0,i;
char arr[100];
cout<<"enter the number"<<endl;
cin>>n;
cout<<"enter the base"<<endl;
cin>>base;
if(base<2 || base>16)
{
cout<<"please enter valid base between 2 to 16 including 2 and 16 "<<endl;
return 0;
}
while(n!=0)
{
temp=n%base;
if(temp<10)
{
arr[count]=temp+48;
}
else
{
arr[count]=temp+55;
}
n=n/base;
count++;
}
cout<<"converted value is"<<endl;
for(i=count-1;i>=0;i--)
{
cout<<arr[i];
}
return 0;
}
This is snapshot of above code
This is snapshot of output for above code
----------------------------------------------------------------------------------------------
CODE 2: For given code ,The valid base are between 2 to any base.
#include<iostream>
using namespace std;
int main()
{
int base,n,temp,count=0,i;
int arr[100];
cout<<"enter the number"<<endl;
cin>>n;
cout<<"enter the base"<<endl;
cin>>base;
if(base<2)
{
cout<<"please enter valid base greater than 1"<<endl;
return 0;
}
while(n!=0)
{
temp=n%base;
arr[count]=temp;
n=n/base;
count++;
}
cout<<"converted value is"<<endl;
for(i=count-1;i>=0;i--)
{
cout<<arr[i];
}
return 0;
}
This is snapshot of above code.
This is snapshot of output for above code
Hope it helps.
Still if you have any doubt or need help, please let me know in comment section and if you like, Please upvote.
Thank You