Thread: recoursive function-is this possible??

  1. #1
    Eyal Cohen

    Unhappy recoursive function-is this possible??

    i need to write a recoursive function that gets an integer and returns it reversed!!!
    is it possible?????
    here is my lousy try!! please help me!! its 4 my university and i cant find a way.
    thanks in advance.

    int revnum(int num)
    if (num/10==0)
    return num%10;
    return 10*(num%10)+revnum(num/10,dbl);

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    If you are purely using type int instead of representing the results as a string then this should be good enough:
    unsigned int revnum(unsigned int num)
        unsigned int base = 1;
        // determine the ten base value
        while ((num / base) >= 10)
        if (num >= 10)
            num = ((num % 10) * base) + revnum(num/10);
        return num;
    Of course, realize that using type unsigned int (4 bytes), restricts the size of the number you can use. If you enter a value that exceeds 4228250624, then you will get bogus data back.

    Last edited by Strider; 12-21-2001 at 12:04 PM.
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001


    if it helps at all......reverse of a number:


    I figured this little equation out all on my own. What do you think about it?

  4. #4
    Registered User
    Join Date
    Dec 2001
    If you know how to turn a number into a string, try to figure out how to print a string backwards. Even if you don't, it's easier to think it through for a string.

    Clue: if you print the letter before the recursive call, you print forwards; if you print after the call, you print backwards.

    BTW, it's spelled recursive.
    Good luck, Al

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. recoursive function
    By Mecnels in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2002, 05:27 PM