write a recursive function to convert a number in decimal to a given base b, where b is between 2 and 36.

so far converts between 2 and 16 what must be added to convert between 2 and 36?

#include <iostream>

using namespace std;

void toBase(int n, int base)

{
    	string alpha="0123456789ABCDEF";
    	if (n > 0)
        	{
        		toBase(n/base,base);
        		cout << alpha[n%base];
        	}
}

    main()

{
        	int num, b;
        	cout << "Input number and the Base to Convert it to: ";
        	cin >> num >> b;
        	cout << endl << num << " in base 10 is ";
        	toBase(num,b);
        	cout << " in base " << b << endl;
}

<< moderator edit: added [code][/code] tags >>

Recommended Answers

All 3 Replies

>> what must be added to convert between 2 and 36?
How about using all letters of the latin alphabet rather than just the first 6?

is that really all I was missing? Thank you the table I have only goes up to 16 and I am not familiar with bases

Think about it like this. Base 2 has two digits, 0 and 1. Base 8 has eight digits, 0 through 7. Base 10 has 0 through 9, and base 16 has 0 through 10 including the first six letters of the alphabet. In other words, the number of digits matches the base. If you want base 36, take note that there are 26 letters in the latin alphabet, and ten decimal digits. The total is 36, so at the very least you need to add that to your function to represent base 36.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.