954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

recursive function

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 >>

madt
Newbie Poster
16 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

>> 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?

Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

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

madt
Newbie Poster
16 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You