can someone please tell me what is wrong with the code

This is a discussion on can someone please tell me what is wrong with the code within the C Programming forums, part of the General Programming Boards category; a program to print n terms of Fibonacci series using recursion. the problem is that when am executing the code ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Question can someone please tell me what is wrong with the code

    a program to print n terms of Fibonacci series using recursion.
    the problem is that when am executing the code the loop is not stopping where it should...
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void recurring_fibonaucci(int ,long int ,long int );
    int main()
    {
        int len;
        printf("\t\t\tFibonaucci series\n\nlength of the series should be between 1 to 47.");
        do {
            printf("\nenter the length of series :");
            scanf("%d",&len);
            printf("\n");
        }while(!(len>1&&len<47));//length should be between 1 and 47
    
        printf("\n\ngenerated number(s) via Recurion\n");
        long int r_m=0,r_n=1;
        printf("%ld\t",r_m);
        recurring_fibonaucci(len ,r_m ,r_n);
    
        getch();
        return 0;
    }
    
    void recurring_fibonaucci(int len,long int r_m,long int r_n)  {
    
    //    printf("\nlength: %d\n\n",len);
        while(len>1) {
            printf("%ld\t",r_n);
            len=len-1;
            recurring_fibonaucci(len ,r_n ,r_m+r_n);
        }
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well... how about some hint of what you expect and what you're getting?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    fibonaucci series: 0, 1, 1, 2, 3, 5, 8, 13,.....i.e the first no. is 0 and the second no is 1 and after that the nth digit is the sum of (n-1)&(n-2).
    so in the program if the input is 6,i.e the length of series is 6 then the series should be ---> 0, 1, 1, 2, 3, 5
    but obviously, something is wrong

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    You're mixing iteration with recursion. What you should be doing is choosing either iteration or recursion.

    But you still didn't answer CommonTater's questions:

    * what do you expect
    * what do you get
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MWAAAHAAA View Post
    You're mixing iteration with recursion. What you should be doing is choosing either iteration or recursion.
    Actually... recursion should be a last resort thing for when there's just no other way. It's on the "no no" list right behind "Self-modifying code".

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    the prog. works...i suppose it only recurion and not a mix
    Code:
    //Write a program to print n terms of Fibonacci series using recursion
    #include<stdio.h>
    
    
    int recurring_fibonaucci(int ,long int ,long int );
    int main()
    {
        int len;
        printf("\t\t\tFibonaucci series\n\nlength of the series should be between 1 to 47.");
        do {
            printf("\nenter the length of series :");
            scanf("%d",&len);
            printf("\n");
        }while(!(len>1&&len<47));//length should be between 1 and 47
    
    
        printf("\n\ngenerated number(s) via Recurion\n");
        long int r_m=0,r_n=1;
        printf("%ld",r_m);
        recurring_fibonaucci(len-1 ,r_m ,r_n);
    
        getch();
        return 0;
    }
    
    int recurring_fibonaucci(int len,long int r_m,long int r_n)  {
        //int r_l = len;
        //printf("\nlength: %d\n\n",len);
        printf("\t%ld",r_n);
        if(len>1)
            recurring_fibonaucci(len-1 ,r_n ,r_m+r_n);
        else
            return 0;
    };

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    all rite...

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by CommonTater View Post
    Actually... recursion should be a last resort thing for when there's just no other way. It's on the "no no" list right behind "Self-modifying code".
    As pointed out in another thread, the matter is not as black-and-white when practical considerations are taken into account.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 12:52 PM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

Tags for this Thread


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