How would I print the number of operations and time taken to compute Fibonacci numbers recursively versus that needed to compute them iteratively. Here is my code currently:
#include<bits/stdc++.h>
using namespace std;
int iterative(int n){
int num1 = 0, num2 = 1, num3, j;
cout<<num1<<" "<<num2<< " ";
if( n == 0)
return num1;
for (j = 2; j <= n; j++){
num3 = num1 + num2;
cout<<num3<<" ";
num1 = num2;
num2 = num3;
}
return num2;
}
int recursive(int num){
if (num <= 1)
return num;
return recursive(num-1) + recursive(num-2);
}
int main (){
// main method to test the function
int n;
cout << "Enter a value for n: ";
cin >> n;
printf("\nWhen n is %d ,the iterative method number is : %d", n,iterative(n));
getchar();
printf("\nWhen n is %d ,the recursive method number is : %d", n,recursive(n));
getchar();
return 0;
}