recursion!!!

This is a discussion on recursion!!! within the C Programming forums, part of the General Programming Boards category; below i am trying to print fibonacci series using recursion. Code: #include<stdio.h> #include<conio.h> int fib(int); int main() { fib(5); getch(); ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    recursion!!!

    below i am trying to print fibonacci series using recursion.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int fib(int);
    
    int main()
    {
        fib(5);     
        getch();
    }   
      
    int fib(int n)
    {   int  a;
     
        if(n==0||n==1)
          return 1;
        
        else
          a=fib(n)+fib(n-1);
          printf("%d\t",a);
         
        return a;
    }
    i am getting output as: 2 3 2 5 2 3 8
    which is wrong

    i am not able to figure out how recursion works in the above program???

    thanks
    samir

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Inside fib, you have fib(n) calling the function fib(n). Which will in turn call fib(n), which will call fib(n), which will call fib(n). Then this function will call fib(n), which will proceed to call fib(n). At which point fib(n) will be called.

    Perhaps you meant
    Code:
    a = fib(n - 1) + fib(n - 2);

  3. #3
    Registered User Bitojis's Avatar
    Join Date
    Jun 2005
    Posts
    20
    Here you have the code of a Fibonacci progresion.

    Code:
    //---------------------------------------------------------------------------
    
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    #include <stdio.h>
    #pragma argsused
    int main(int argc, char* argv[])
    {
    int n, a, b, c;
    
    printf("Introduzca el numero de terminos: ");
    scanf("%i", &n);
    
    do {
    a=1;
    b=1;
    printf("%i, ", a);
    c++ ;
    }while (c<2);
    
    do{
    a=(a+b);
    b=a;
    printf("%i, ", a);
    c++;
    }while (c<n);
    
    getchar();
    return 0;
    
    
            return 0;
    }
    //---------------------------------------------------------------------------

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Here you have the code of a Fibonacci progresion.
    Poorly written, non-portable, broken code with no formatting that fails to recognize that everyone here communicates in English. I fail to see how your post is anything more than an attempt to boost your post count without having to think.
    My best code is written with the delete key.

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, 08:45 PM
  2. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 09: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, 08: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, 01:29 PM

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