on Leave a Comment

C++ Program for Fibonacci Series using Recursion

This article contains C++ Program for Fibonacci Series using Recursion.

Recursion: When a function calls itself, it is called recursion.

Fibonacci series example, 0       1       1       2       3       5

C++ Program for Fibonacci Series using Recursion

#include <iostream>
using namespace std;
int fibonacci(int n);
int main()
{
    int n,i=0;
    cout<<"Enter The Terms For Fibonacci Series : ";
    cin>>n;
    cout<<endl<<"Fibonacci Series : "<<endl<<endl;
    while(i<n)
    {
        cout<<fibonacci(i)<<"\t";
        i++;
    }
    return 0;
}
int fibonacci(int n)
{
    if((n==0) || (n==1))
    {
        return n;
    }
    else
    {
        return(fibonacci(n-1)+fibonacci(n-2));
    }
}

OUTPUT:

Enter The Terms For Fibonacci Series : 10

Fibonacci Series :

0       1       1       2       3       5       8       13      21      34

0 comments:

Post a Comment