[Resolved] Recursion: Reverse an Integer
I need to write a recursive function to reverse the order of digits in an integer. This is a homework problem, so it has to be done using recursion (not a loop or anything). This is what I've come up with so far:
Code:
public int revDigits (int num) {
if (num < 10) {
return num;
} else {
int s = num / 10;
int d = num % 10;
int i = revDigits(s);
return (d * 10) + i; // incorrect
// need to find out how many digits in d:
//return (d * 10^(num digits in d)) + i);
}
}
The problem that I've come across is commetted at the bottom. Any help with solving this would be appreciated. Thanks in advance.