Fibonacci Series With Recursion Process .

 

                Fibonacci Sequence

 

The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the receding two numbers.


fibonacci series with recursion in cpp


Note:

 

This Can Be Done Very Easily Iteratively but It Takes More Time with Recursive Function, Best Approach Is to Do This Task Iteratively.

       C++ Code With Recursion:

 

 

#include <iostream>

#include<chrono>    //Library For Calculating Time

 unsigned fib(unsigned);  //Function Declaration

 using namespace std;

 int main(){

     unsigned size , e=0;

     cout<<"\t\t\t\tFibonacci Series Using Recursion Method !";

     cout <<endl<< "Enter The Size  Of Series : ";

     cin >> size;   //Input Size Of Series

     auto q=chrono::steady_clock::now();

     cout << "\nFibonnaci Series : "<<endl<<"\t\t";

     while(e < size) {

     cout << " " << fib(e);    //Function Calling

     e++;

}

     auto r=chrono::steady_clock::now();

 

     unsigned time=chrono::duration_cast<chrono::nanoseconds>(r-q).count();//Calculating Time

  cout<<endl<<endl<<endl<<"Execuation Time : "<<time<<" nanosecond(ns)";

     return 0;

}

unsigned fib(unsigned  x) 

{

   if((x==1)||(x==0))  

{

      return(x);

}else{

      return(fib(x-1)+fib(x-2));

}

}

 

Post a Comment

0 Comments