1 Write an iterative C function that inputs a nonnegative in

1. Write an iterative C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number. 2. Write a recursive C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number. 3. Compare the number of operations to compute Fibonacci numbers recursively versus that needed to compute them iteratively.

Solution

#include <stdio.h>
#include<conio.h>
#include<iostream.h>
int fibo(int initial_value,int final_value,int count,int &recounter)

   {

    int temp;
   
temp=initial_value+final_value;
   if (temp<=count)

    {
       
       initial_value=final_value;
       final_value=temp;
       cout<<temp<<endl;
recounter++;
       fibo(initial_value,final_value,count,recounter);
    }
else
return(0);
}
int main()
{
int initial_value=0,final_value=1,temp_value,count,ini=0,fin=1,final;
// count contains the number of elements to be generated.....
cout<<\"Enter Number of Series\";
cin>>count;
   int whilecounter=0,recounter=0;
   temp_value=initial_value+final_value;
   cout<<initial_value<<endl;
     
   while(count>temp_value)
{
cout<<temp_value<<endl;
temp_value=initial_value+final_value;
initial_value=final_value;
final_value=temp_value;
whilecounter++;
    }
   cout<<\"Number of Iterations Using Non Recursive\"<<whilecounter<<endl;
   cout<<ini<<endl;
    cout<<fin<<endl;
   final=fibo(0,1,count,recounter);
   cout<<\"Number of Iterations Using Recursive\"<<recounter<<endl;
getch();
}

1. Write an iterative C++ function that inputs a nonnegative integer n and returns the nth Fibonacci number. 2. Write a recursive C++ function that inputs a non

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site