Tuesday, December 15, 2009

Converting numbers to base 5 recursively in Java?

Could somebody help me write a method that converts a non negative integer (base 10) into base 5? For example, calling the method and sending 5 would return 10. Much appreciated.Converting numbers to base 5 recursively in Java?
I will give you an example of converting a decimal number into an octal form. You can easily apply the same technique to any base.





Here's how to convert 12345 to octal base.





12345 = 8 * 1543 + 1


1543 = 8 * 192 + 7


192 = 8 * 3 + 0


24 = 8 * 3 + 0


3 = 8 * 0 + 3





The octal expansion of 12345 is equal to 30071





Such algorithm is easily solved with recursion. All you have to do is convert it into a code.

No comments:

Post a Comment