Thread: simple Recursion program - strange output

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    68

    simple Recursion program - strange output

    i want to write program which prints all natural numbers from 1 to n.
    where n is user input. but i want to write using recursion and not by for loop.
    ex. if user enters 5 then output should be 1 2 3 4 5
    i tried following code but its not working.

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main()
    {
        int n;
        printf("enter no.");
        scanf("%d", &n);
        rec(n);
        return 0;
    }
    
    
    int rec(int n) {
        if(n==1) {
            printf("1\n");
        }
        else {
                n--;
                printf("%d\n",rec(n));
        }
    }
    the output is 1 0 2 2 2....upto n.

    please help me and can anyone tell me whats the mistake in this code
    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,070
    You might find it easier to work with:
    Code:
    int rec(int i, int n) {
    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
    Aug 2015
    Posts
    68
    please can you explain me? how to solve further?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,070
    Try writing the for loop version first.
    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

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    68
    yes i have done using for loop
    for(i=1;i<=n;i++)
    printf("%d\n", i);
    but i want to do it using recursion

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,070
    Yep, and you even named the variables the same way. Implement the recursive version then. You just need to invert the loop condition to be your recursive base case (or alternatively, you can put the entire body of the function within the body of the if statement).

    Actually, since you are only printing, you don't need to return an int, i.e., change rec's return type to void.
    Last edited by laserlight; 4 Weeks Ago at 08:52 AM.
    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

  7. #7
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    147

    Post

    The function you have prints it backwards...
    This is the function actually working.

    Code:
    void rec(int n) {
        if(n > 1) { 
            printf("%i\n",n);
            rec(n-1);
        } else { 
            printf("1\n");
        };
    };
    can anyone tell me whats the mistake in this code
    thanks
    Code:
     n--; 
     printf("%d\n",rec(n));
    Last edited by Structure; 4 Weeks Ago at 09:01 AM.


  8. #8
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    147
    if user enters 5 then output should be 1 2 3 4 5
    Code:
    #include <stdio.h>
    Code:
    void rec(int i, int n) {
        printf("%i\n",i);
        if(n > i) {
            i++;
            rec(i,n);
        };
    };
    Code:
    int main() {
        int n;
        printf("enter no.");
        scanf("%d",&n);
        rec(1,n);
        return 0;
    };
    Last edited by Structure; 4 Weeks Ago at 09:49 AM.


  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,070
    Structure: don't do other people's homework before they have succeeded.
    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

  10. #10
    Registered User
    Join Date
    Aug 2015
    Posts
    68
    Quote Originally Posted by Structure View Post
    Code:
    #include <stdio.h>
    Code:
    void rec(int i, int n) {
        printf("%i\n",i);
        if(n > i) {
            i++;
            rec(i,n);
        };
    };
    Code:
    int main() {
        int n;
        printf("enter no.");
        scanf("%d",&n);
        rec(1,n);
        return 0;
    };
    Hey Thanks man. this code is working !!

  11. #11
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    147
    this code is working
    Code:
      rec(100,150);
    Last edited by Structure; 4 Weeks Ago at 11:25 AM.


  12. #12
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    147
    Code:
    void numbers(int one, int two) {
      printf( "%i\n", one );
      if ( one != two ) {
        if ( one > two ) {
          one--;
        } else if ( two > one ) {
          one++;
        };
        numbers(one,two);
      };
    };
    structure:
    Recursion (computer science) - Wikipedia
    Last edited by Structure; 4 Weeks Ago at 11:25 AM.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program output is strange characters, totally baffled
    By chronograff in forum C Programming
    Replies: 4
    Last Post: 05-16-2017, 06:43 AM
  2. Whats wrong with simple recursion printing program?
    By tmac619619 in forum C Programming
    Replies: 6
    Last Post: 09-02-2015, 02:41 PM
  3. Replies: 1
    Last Post: 03-16-2015, 10:36 AM
  4. Help: Really strange output for simple calculation.
    By gpix13 in forum C Programming
    Replies: 4
    Last Post: 01-16-2010, 08:27 AM
  5. Very strange behavior from very simple program
    By somekid413 in forum C Programming
    Replies: 5
    Last Post: 12-17-2009, 08:53 PM

Tags for this Thread