Computer Science, asked by sakshikesralikar2001, 6 months ago

write a c++ program to calculate Fibonacci series using both recursion and non recursion in one program .​

Answers

Answered by thasthakir47
1

Answer:

Using Recursion:

#include<iostream>  

using namespace std  

int main() {  

  //Variable declaration  

  int n;  

  cin >> n;  

  int a[n];  

 

  //Initialise Fibonacci  

  a[0] = 0;  

  a[1] = 1;  

  cout << a[0] << endl;  

  cout << a[1] << endl  

 

  //Iteration  

  for(int i = 2; i < n; i ++) {  

     a[i] = a[i-1] + a[i-2];  

     cout << a[i] << endl;  

  }  

}  

Non- Recursion:

#include <iostream>

using namespace std;

int main()

{

int num1 = 0;

int num2 = 1;

int num_temp;

int num_next = 1;

int n;

cin >> n;

if (n>=1)

cout << num1 << " ";

if (n>=2)

cout << num2 << " ";

for (int i = 0; i < n-2; i++)

{

num_next = num1 + num2;

cout << num_next << " ";

num1 = num2;

num2 = num_next;

}

cout << endl;

return 0;

}

Explanation:

Similar questions