A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values, and which obtains the result for the current input by applying simple operations to the returned value for the smaller (or simpler) input.

RECURSIVE ALGORITHM

Aim:

                  To write a C++ program to generate  Fibonacci series using  Recursive Algorithm.

Algorithm:

Step1: Define a function fibonacci(n) which returns Fn.

Step 2: If n==0 then Fn returns 0.

Step 3: If n==1 Fn returns 1.

Step 4: If (n>1) Fn is generated by fibonacci(n-1)+fibonacci(n-2) .

Step 5: Print the nth fibonacci term.


               

  RECURSIVE ALGORITHM PROGRAM

#include<iostream>
using namespace std;
int fibonacci(int n)
{
    if((n==1)||(n==0))
    {
        return (n);
    }
    else
    {
        return(fibonacci(n-1)+fibonacci(n-2));

    }
}
int main()
{
    int n,i=0;
    cout<<"\n Input the number of terms for fibonacci series:";
    cin>>n;
    cout<<"\n Fibonacci series is as follows \n";
    while(i<n)
    {
        cout<<" "<<fibonacci(i);
        i++;
    }
    return 0;
}


OUTPUT:



BY
      REGU RAM SV

Post a Comment

Previous Post Next Post