Recursion problem

This is a discussion on Recursion problem within the C Programming forums, part of the General Programming Boards category; How can i use recusion to get the below number, the number should not in a fixed length....thanks eg. input ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Recursion problem

    How can i use recusion to get the below number, the number should not in a fixed length....thanks

    eg. input 3456
    first time get : 3
    second time get : 34
    third time get : 345

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Sniff sniff...do i smell homework??
    Post what you have written and the people here will help you in fixing your code.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    below is my code, but it can get the first digit only????thanks

    Code:
    int getFirst(int n) 
    {     int rtn;
           if (n<0)
             return n;
           rtn = getFirst(n/10);
           return rtn;
    }

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    That is not going to give you the first digit..n will never be less than 0.
    You might want to use a for loop to get one or two or three digits and so on..i would send another variable 'count' along with n to the function telling it to return n when n/(int)pow(10,count) is zero. So, then for your number, it would return 3, then 34, then 345 and then 3456.

    count would start with 1 and go on till whatever the number of digits in the number are.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

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 problem
    By trnd in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 03:06 PM
  3. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 04:52 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. recursion problem
    By dustinc in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 04:29 AM

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