Need answer in c++ programming using classes The Fibonacci sequence is the serie
ID: 3595726 • Letter: N
Question
Need answer in c++ programming using classes The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89… See the pattern? Each element in the series is the sum of the preceding two elements. Here is a recursive formula for calculating the nth number of the sequence: The Fibonacci sequence is the series of integers 0 a. Write a recursive method fibonacci that returns the nth Fibonacci number when passed the argument n. b. Write a non recursive version of the method fibonacci. c. Write a driver to test your two versions of the method fibonacci.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int recFibonacci(int n)
{
if (n <= 1)
return n;
return recFibonacci(n-1) + recFibonacci(n-2);
}
int NonecFibonacci(int n)
{
int c=0;
int a = 0;
int b = 1;
for (int i = 2; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return c;
}
int main() {
int n;
cout<<"Enter the n: "<<endl;
cin >> n;
cout<<n<<" fibonacci number by recursion is "<<recFibonacci(n)<<endl;
cout<<n<<" fibonacci number by non-recursion is "<<NonecFibonacci(n)<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.