Rewrwrtie the following C++ code to be able to take any decimal number and conve
ID: 3555068 • Letter: R
Question
Rewrwrtie the following C++ code to be able to take any decimal number and convert it to vegesimal number(to be used with linked list) *I have gotten this to work with numbers up to 600 but I know that there must be a loop that you can use on it. Can someone please figure this out for me. This is what I have so far:
#include <iostream>
using namespace std;
void Yea(int num);
int main()
{
char ch;
char response;
int num;
cout << "Decimal Number: ";
cin >> num;
Yea(num);
cout << "Run again?: ";
cin >> response;
cout << endl;
while((response=='Y')||(response=='y'))
{
cout << "Decimal Number: ";
cin >> num;
Yea(num);
cout << "Run again?: ";
cin >> response;
cout << endl;
}
system("PAUSE");
return 0;
}
void Yea(int num)
{
char ch;
char ch2;
char ch3;
int div;
int rem;
int temp;
bool i;
i=false;
div=num/20;
rem=num%20;
cout << "Vigesimal number is: ";
if(div>19)
{
temp=(div/20);
num=(num%200);
ch3=(48+temp);
cout << ch3;
i=true;
}
else
;
if(i==true)
{
div=num/20;
rem=num%20;
}
else
;
if((rem>9)&&(div<10))//Number out front(ch) & letter in back(ch2)
{
ch=(48+div);
ch2=(55+rem);
}
else if((div>9)&&(rem<10))//Letter out front(ch) & number in back(ch2)
{
ch=(55+div);
ch2=(48+rem);
}
else if((div>9)&&(rem>9))//Letter out front (ch) & letter in back(ch2)
{
ch=(55+div);
ch2=(55+rem);
}
else //Number out front(ch) & number in back (ch2)
{
ch=(48+div);
ch2=(48+rem);
}
cout << ch << ch2 << endl << endl;
}
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
void Yea(int num);
int main()
{
char ch;
char response;
int num;
cout << "Decimal Number: ";
cin >> num;
Yea(num);
cout << "Run again?: ";
cin >> response;
cout << endl;
while((response=='Y')||(response=='y'))
{
cout << "Decimal Number: ";
cin >> num;
Yea(num);
cout << "Run again?: ";
cin >> response;
cout << endl;
}
system("PAUSE");
return 0;
}
void Yea(int num)
{
char ch;
char ch2;
char ch3;
int div;
int rem;
int temp;
bool i;
string ans="";
i=false;
cout << "Vigesimal number is: ";
while(num>0){
rem=num%20;
if(rem>9)ans=ans+(char)(65+rem-10);
else ans=ans+(char)(48+rem);
num=num/20;
}
for(int i=ans.length()-1;i>=0;i--)cout<<ans[i];
cout<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.