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)?