Thread: Recursion...

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    1

    Recursion...

    Hello, I am still noob at programming
    Our lesson is about recursion
    I know about basic stuff about how to do the Fibonacci Sequence and factorial

    However, our teacher has been making us to do complex stuff
    like:

    If N = 5:

    12345
    *123*
    **1**

    If N = 7:
    1234567
    *12345*
    **123**
    ***1***

    and

    If N = 5:
    12345
    *2345
    **345
    ***45
    ****5

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    See if you can do this with a loop. Then just about any loop can be converted into recursion if you pass enough of the variables as parameters. For the first 2 examples, you would pass the original value of N, and a current value that decrements by 2 on each recursion.
    Last edited by rcgldr; 11-24-2015 at 09:35 AM.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    forum bug, I tried to edit reply and it made a second instance. Tried to delete the first, but that didn't work either, so I'm leaving both replies.

    See if you can do this with a loop. Then just about any loop can be converted into recursion if you pass enough of the variables as parameters. For the first 2 examples, you would pass the original value of N, and a current value that decrements by 2 on each recursion.
    Last edited by rcgldr; 11-24-2015 at 09:36 AM.

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    something like this for the last one maybe

    Code:
    #include<stdio.h>
    
    void func(int n, int count){
    if(count==n)
    return;
    int i;
    for(i=1; i <= n; i++){
    if(i<=count)
    printf("*");
    else
    printf("%i", i);
    }
    printf("\n");
    func(n, count+1);
    }
    int main(void){
    func(5,0);
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with recursion
    By sick in forum C Programming
    Replies: 7
    Last Post: 07-20-2009, 03:15 PM
  2. Help about recursion
    By rohanpandit in forum C Programming
    Replies: 4
    Last Post: 06-30-2009, 04:00 PM
  3. Recursion help please?
    By FusedOut in forum C Programming
    Replies: 4
    Last Post: 09-07-2003, 06:01 AM
  4. Recursion
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-21-2003, 06:20 PM
  5. Recursion
    By Peaches in forum C Programming
    Replies: 7
    Last Post: 11-22-2002, 09:09 AM