Monday, 19 August 2013

Generating Fibonacci Series with user defined lower and upper limits in C++

#include<iostream>

using namespace std;

int Fibonacci (int);                                     //Function prototype declaration

int main(void)
{
int m,n,i;

cout<<"\nEnter the lower bound : ";
cin>>m;

cout<<"\nEnter the upper bound : ";
cin>>n;
       
        if(m>=n)  
cout<<"\nInvalid Range! Check your boundary values. ";  //Checking range validity

else
for(i=m; i<=n; i++)
cout<<Fibonacci(i)<<"  ";     //Calling the Fibonacci function
cout<<"\n";
return 0;
}

int Fibonacci (int number)
{
if (number==0)                                         //First number is 0
return 0;
if (number==1)                                        //Second number is 1
return 1;
else
return Fibonacci(number-1) + Fibonacci(number-2);  //Formula for number >=2
}