I have this recursive function to reverse a positive integer. Anybody having an efficient algorithm to do the task using fewer recursive calls (but still using recursion), please post here!
int countDigit (const int & _X) {
if (_X < 10)
return 1;
return (countDigit(_X / 10) + 1);
}
int getPower (const int & _X, const int & _Y) {
if (_Y == 0)
return 1;
int ans = getPower(_X, _Y / 2);
if (_Y % 2)
return _X * ans * ans;
return ans * ans;
}
int reverseDigit (const int & digit) {
if (digit < 10) {
return digit;
}
int num = (digit % 10) * getPower(10, countDigit(digit / 10));
return num + reverseDigit(digit / 10);
}