Recursion help please?

This is a discussion on Recursion help please? within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm a newbie to c programming.... having trouble with recursion now.... how do i write a recursive function ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    1

    Recursion help please?

    Hi everyone,

    I'm a newbie to c programming.... having trouble with recursion now....

    how do i write a recursive function that prints a positive integer in reversed order? for example: toReversed(345) will display 543.

    any tips to attempt doing recursive functions? what are the basic steps to do recursion? any tips for me to write out the algorithm?

    thanks everyone!

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    unless this is a homework assignment where you have to use it, recursion isn't the best way to do what you want. It would be worth it to look through the math and string function headers.

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    try a while() loop...
    Away.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Perhaps this gives an impression of what you could do:

    345 % 10 = 5
    345 - 5 = 340
    340 / 10 = 34

    34 % 10 = 4
    34 - 4 = 30
    30 / 10 = 3

  5. #5
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532
    You may do it this way:

    Code:
    //Reverse function
    void preversed(int num)
    {
    int i;
                                                                                                                                 
    if(num >= 10 )
            {
            i = num%10;
            printf("%d",i);
            preversed(num/10);
            }
    else
            printf("%d",num);
                                                                                                                                 
    }
                                                                                                                                 
                                                                                                                                 
    
    int main()
    {
    int num;
    printf("Enter your number:");
    scanf("%d",&num);                 //Collect number
                                                                                                                                 
    printf("Number reversed  :");
    preversed(num);                    // Call the function
    printf("\n");
                                                                                                                                 
    return 0;
    }
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 10:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21