C++ code:
Make a recursive function which displays the fibonacci series before a number that is entered by the user.
Requirements:
No global declarations
Test run in main
Diagram:
Also draw diagram to show how the recursive call is working
using namespace std;
int FibonacciSeries(int n)
{
if((n==1)||(n==0)) return(n);
else return(FibonacciSeries(n-1)+FibonacciSeries(n-2));
}
int main()
{
int num , i=0;
cout << "Enter a number (>=0): "; cin >> num;
cout << "\nThe Fibonnaci Series : ";
while(FibonacciSeries(i) <= num)
{
cout << " " << FibonacciSeries(i);
i++;
}
return 0;
}
Comments
Leave a comment