fibonci series and recursive function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hapa
    New Member
    • Oct 2006
    • 31

    fibonci series and recursive function

    i was looking for questions on while loops when i came across a problem that said write a program that displays fibonacci numbers
    and the code that i wrote works just fine

    Code:
    int x1=1;
    int x2=1;
    int x3;
    int x;
    cout<<"please enter the number "<<endl;
    cin>>x;
    cout<<x1<<endl<<x2<<endl;
    while (x2<x)
    {
        x3=x1+x2;
        x1=x2;
       x2=x3;
      cout<<x1<<endl<<x2<<endl;
    
    }
    but the second part of the question says write another program but this time using recursive funtion
    can any body please explain that to me and also how can i change my program to become a recursive function
    i can write this as a seprate funtion
    but how to find the terminating if function and how to approach this problem using recursive I have an exam coming up please help as soon as possible
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by hapa
    but the second part of the question says write another program but this time using recursive funtion
    can any body please explain that to me and also how can i change my program to become a recursive function
    i can write this as a seprate funtion
    but how to find the terminating if function and how to approach this problem using recursive I have an exam coming up please help as soon as possible
    The algorithm for a recursive version of function fibonacci(int i) is
    Code:
      if (i <= 2) return 1;
      else return fibonacci(i-1) + fibonacci(i-2);
    the recursion terminates when the function parameter i becomes less than or equal to 2
    see the folowing for a discussion on binary recursion

    Comment

    • hapa
      New Member
      • Oct 2006
      • 31

      #3
      Originally posted by horace1
      The algorithm for a recursive version of function fibonacci(int i) is
      Code:
        if (i <= 2) return 1;
        else return fibonacci(i-1) + fibonacci(i-2);
      the recursion terminates when the function parameter i becomes less than or equal to 2
      see the folowing for a discussion on binary recursion
      http://www.csse.monash.edu.au/~lloyd...S/Recn/Binary/
      but shoudlnt the function have 2 input argument

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by hapa
        but shoudlnt the function have 2 input argument
        no the function header would be
        Code:
        int fibonacci(int i)
        it shoud return the i'th fibonacci number

        Comment

        • hapa
          New Member
          • Oct 2006
          • 31

          #5
          Originally posted by horace1
          no the function header would be
          Code:
          int fibonacci(int i)
          it shoud return the i'th fibonacci number

          Thank you it works
          i understand it now

          done it and it works

          Comment

          Working...