Hello all,
Basically I'm trying to code a program that makes use of recursion to reverse an input integer, e.g. 123 will result in a display of 321.
I've coded the program this far, but it just displays the last digit only. Where have I gone wrong? I.e. Instead of 123 displaying 321, it displays just 3. I have done printf debugging, and my issue is how to make the program remember the first few digits and print them out as well, e.g. Rmb 2 and 1, and display these integers together with the 3 that is already printed.
Here's what I've done for the reverseDigits function:
void reverseDigits(int n, int *result) {
int reverseNum = 0;
int basePos = 1;
if(n == 0) {
printf("\n");
}
if(n != 0) {
reverseDigits(n/10, result);
reverseNum += (n%10)*basePos;
basePos *= 10;
printf("REVERSE NO.: %d\n\n", reverseNum);
}
*result = reverseNum;
}
Thanks!