#include #include /* The algorithm, as you learned in 125 is continually compute the remainder and the quotient of the number and the base until the quotient is less than the base. Add the remainder to the new number each time After the quotient is less than the remainder add the quotient to the new number. Print the new number in reverse order. Note: We will use the fact that the digits in ascii are sequential '0' + 0 = '0' '0' + 1 = '1' ... We will use character/string concatenation in reverse order to keep track of the new number. */ using namespace std; int main() { int n, base; string result; int remainder, quotient; bool validBase = false; cout << "Enter a number to convert => "; cin >> n; // force a valid base. while (not validBase) { cout << "Enter a base between 2 and 10 inclusive => "; cin >> base; cout << endl; if (base < 2 or base > 10) { cout << base << " is not between 2 and 10." << endl; } else { validBase = true; } } cout << endl; // do the conversion algorithm quotient = n; while (quotient >= base) { // do the division remainder = quotient % base; quotient /= base; // add remainder onto the result string // this is a bit of a trick, make sure you understand this. result = static_cast(static_cast('0') + remainder) + result; } if (quotient > 0 or result.size() == 0) { // add the quotient onto the result string result = static_cast(static_cast('0') + quotient) + result; } // print the results. cout << n << " in base " << base << " is " << result << endl; cout << endl; return 0; }