I have an assigment to create a function that recieves a number, and return the reversed number.
E.G :
Input : 12343
Output : 34321
No loops allowed, and the input is only the number .
This is what I've tried :
long GetReverse(unsigned long n)
{
if (n < 10)
return n % 10;
else
return 10 * GetReverse(n / 10) + n % 10;
}
Though This is retuning me the same input and not reversing the number ( I know what is the problem here, I just can't think of a way to do it)
Any thoughts?
EDIT: This is the solution I came up with :
int numOfMulti(unsigned long num) {
if (num < 10)
return 1;
return 10 * numOfMulti(num / 10);
}
long GetReverse(unsigned long n)
{
if (n < 10)
return n % 10;
else
return n % 10 * numOfMulti(n) + GetReverse(n / 10) ;
}
Wasn't able to find a solution without a secondary function or static variables.