Problem Statement 2 The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, … Begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. Write a recursive function that generates N series numbers. Where N is the integer inputted by the user at runtime.
#include<iostream>
using namespace std;
int Fibonacci (int number)
{
if(number == 0){
return 0;
}
if(number == 1 || number == 2){
return 1;
}
return Fibonacci(number-2) + Fibonacci(number-1);
}
int main ()
{
cout<<"Enter N: "<<endl;
int n;
cin>>n;
for(int i=0; i<n; i++){
cout << Fibonacci(i)<<" ";
}
getchar();
return 0;
}
Comments
Leave a comment