Tuesday, 17 June 2014

C++ program to generate Fibonacci series


Here are C++ programs to generate Fibonacci series by various methods such as by recursion , by function, by using for loop, by pointers etc. I have also given program to find whether a given number is Fibonacci or not and program to calculate nth number of the Fibonacci series using recursion
In Fibonacci series next number is found by the addition of previous two numbers i.e.
0   1   1   2   3   5   8 …………..
Initial two numbers are taken i.e. 0 and 1, now we will add two numbers and result will be 1 thus this is our 3rd number of series .In this way whole series is generated. 
Here is a simple way to print Fibonacci series.




Method 1 : C++ program to generate Fibonacci series


#include<iostream>
using namespace std;
int main()
{
    int n,a=0,b=1,temp;
    cout<<"Enter value of n : ";
    cin>>n;
    cout<<"Fibonacci series : ";
    for(int i=0;i<n;i++)
    {
        cout<<a<<" ";
        temp=a+b;
        a=b;
        b=temp;
    }
    return 0;
}




Method 2 : C++ program to generate Fibonacci series using function


#include<iostream>
using namespace std;
void fib(int);
int main()
{
    int n;
    cout<<"Enter value of n : ";
    cin>>n;
    cout<<"Fibonacci series : ";
    fib(n);
    return 0;
}
void fib(int n)
{
    int a=0,b=1,temp;
    for(int i=0;i<n;i++)
    {
        cout<<a<<" ";
        temp=a+b;
        a=b;
        b=temp;
    }
}




Method 3 : C++ program to generate Fibonacci series using pointer


#include<iostream>
using namespace std;
int main()
{
    int n,a=0,b=1,temp;
    int *p,*q;
    p=&a;
    q=&b;
    cout<<"Enter value of n : ";
    cin>>n;
    cout<<"Fibonacci series : ";
    for(int i=0;i<n;i++)
    {
        cout<<*p<<" ";
        temp=*p+*q;
        *p=*q;
        *q=temp;
    }
    return 0;
}




Method 4 : C++ program to generate Fibonacci series using recursion


#include<iostream>
using namespace std;
void fib(int n,int a,int b)
{
    if(n==0)
        return;
    cout<<a<<" ";
    fib(n-1,b,a+b);
}
int main()
{
    int n,a=0,b=1,temp;
    cout<<"Enter value of n : ";
    cin>>n;
    cout<<"Fibonacci series : ";
    fib(n,a,b);
    return 0;
}




Method 5 : Another C++ program to generate Fibonacci series using recursion


#include<iostream>
using namespace std;
int fabon(int n);
long y=0,z;
int main()
{
     int n,a[50],i;
     a[1]=0,a[2]=1;
     cout<<"Enter number of terms : ";
    cin>>n;
    if(n<=1)
       cout<<"\n0 1";
    else
    {
        for(i=3;i<n;i++)
        a[i]=a[i-1]+a[i-2]+1;
        z=a[i-1];
        cout<<"\n0 1 ";
        fabon(n-1);
    }
    return 0;
}
int fabon(int n)
{
    int x;
    if(n<=1)
       return n;
    else
    {
       y++;
       x=(fabon(n-2)+fabon(n-1));
       if(y==z)
           cout<<x<<" ";
       return x;
    }
}

Method 5 is quiet complicated. Actually I have simply added cout<<x<<" " but the problem is that if I simply write cout<<x<<" " than during recursive call , with nth series ,(n-1)th series ,(n-2)th series and so on will also get printed .Now to print only the nth series I have used  y variable. We will increment the value of y and when its value is equal to z function print the series .
So before calling the function we will calculate the value of z. Its value will depend on the no. of recursive calls more the number of recursive calls more will be the value of z and we will calculate it using following loop
for(i=3;i<=n;i++)
    a[i]=a[i-1]+a[i-2]+1;
    z=a[i-1];            
where the value of a[1] and a[2] are initialized with 0 and  1.


C++ program to check whether the given number is Fibonacci or not


#include<iostream>
using namespace std;
int main()
{
    int a=0,b=1,temp=0,flag=0,n;
    cout<<"Enter number ";
    cin>>n;
    while(temp<=n)
    {
        temp=a+b;
        a=b;
        b=temp;
        if(temp==n)
            flag=1;
    }
    if(flag==1)
        cout<<"Given number is Fibonacci ";
    else
        cout<<"Given number is not Fibonacci ";
    return 0;
}

OUTPUT





C++ program to calculate nth number of the Fibonacci series using recursion


#include<iostream>
using namespace std;
int fib(int n)
{
    if(n<=1)
        return n;
    else
        return fib(n-1)+fib(n-2);
}
int main()
{
    int num;
    cout<<"Enter the value of n : ";
    cin>>num;
    cout<<num<<"th Fibonacci number is : "<<fib(num);
    return 0;
}

OUTPUT

No comments:

Post a Comment