Program to print the Fibonacci Series using recursion in C++
Solution
#include <iostream> using namespace std; int fibonacci(int i){ if(i==0) return 0; else if(i==1) return 1; else return (fibonacci(i-1)+fibonacci(i-2)); } int main() { int n,i; cout<<"How many elements you want to display "; cin>>n; for(i=0;i<n;i++) { cout<<fibonacci(i)<<"\t"; } return 0; }
Output:
How many elements you want to display 9 0 1 1 2 3 5 8 13 21