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

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    36

    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:

    Printing out a number in reversed order by using recursion + side-question-pointer-png

    *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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Quote Originally Posted by laserlight View Post
    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:
    Printing out a number in reversed order by using recursion + side-question-fak-png

    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
    Printing out a number in reversed order by using recursion + side-question-fibonaci-png

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing LinkList In Reverse Order Using Recursion
    By nickman in forum C Programming
    Replies: 6
    Last Post: 05-06-2014, 11:57 PM
  2. easiet way to put digits at the left side of number
    By cable in forum C Programming
    Replies: 5
    Last Post: 11-04-2011, 05:52 PM
  3. printing number from array question..
    By transgalactic2 in forum C Programming
    Replies: 41
    Last Post: 12-25-2008, 04:04 PM
  4. printing a btree using recursion
    By agerealm in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2003, 08:57 AM
  5. Printing text side by side
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2002, 11:46 AM

Tags for this Thread