# Thread: Printing out a number in reversed order by using recursion + side-question

1. ## Printing out a number in reversed order by using recursion + side-question

Hi!
Printing out a number in reversed order, I was able to do interative:

Code:
```#include <stdio.h>

int digits_reversed_iterative(int num) {
int erg, reversed, digit, i;

reversed = 0;

for (i = 0; num != 0; i++) {
digit = num % 10;
reversed += digit;
reversed *= 10;
num /= 10;
}

reversed /= 10; // letzte iteration "bereinigen"
return reversed;
}

int main() {
int zahl;

printf("Bitte geben Sie eine ganze Zahl ein:\n");
scanf("%d", &zahl);

printf("The reversed output of %d is %d.\n", zahl, digits_reversed_iterative(zahl));
}```
But I am struggling to solve the same task by recursion - here is my attempt, so far:

Code:
```#include <stdio.h>

int digits_reversed_recursive(int num) {
int erg, reversed, digit;
reversed = 0;

if (num == 0) {
return 0;
} else {
return (num % 10);
digits_reversed_recursive(num /= 10);
}
}
int main() {
int zahl;

printf("Bitte geben Sie eine ganze Zahl ein:\n");
scanf("%d", &zahl);

printf("The reversed output of %d is %d.\n", zahl, digits_reversed_recursive(zahl));
}```
Can anyone hint me?

I also would have a quick side-question to whom I think it would be too much to open an extra thread - here it is:

*a and *b are in this case pointers (so variables which have as their value the adresses of the copies of x and y)?
Or are *a and *b in this case already the dereferences (so in this case *a would point to the adresses of the copy of x and use the value of the copy of x)?

2. Originally Posted by Placebo
Can anyone hint me?
Notice that in your iterative solution, you're performing addition and multiplication, but in your recursive solution, you've neglected to perform addition and multiplication.

Originally Posted by Placebo
*a and *b are in this case pointers (so variables which have as their value the adresses of the copies of x and y)?
Or are *a and *b in this case already the dereferences (so in this case *a would point to the adresses of the copy of x and use the value of the copy of x)?
a and b are pointers to int, so *a and *b are ints.

3. Originally Posted by laserlight
Notice that in your iterative solution, you're performing addition and multiplication, but in your recursive solution, you've neglected to perform addition and multiplication.

a and b are pointers to int, so *a and *b are ints.
ah, of course - thanks, that's clear now
About your hint regards to recursion - you have basically pinpointed well my general problem which I have with recursion:
Even regards to the simplest, basic recursion-examples to illustrate things, I have difficulties to realize where the calculation/"math" does happen.

I show you 2 simple example to illustrate my problem:

example 1: faculty:

Ok, here I get that the math which is happening should be:
6 * 5 * 4 * 3 * 2 * 1

But something still feels counterintuitive about it.

example 2: fibonaci

Basically it is a function which gets a copy of the actual-parameter 5 and shall per recursion, output an result.

I have rly no clue what is math-wise happening there, step by step.
It would be helpful for me to understand what is happening there, and after that I would revisit my original posted task.