Modify the recursive function Fibonacci so that each time it is called it prints
ID: 3669074 • Letter: M
Question
Modify the recursive function Fibonacci so that each time it is called it prints out what number call it is as well as the requested Fibonacci number. Write a program that prompts the user for a positive integer, computes the corresponding Fibonacci number, and prints the result. Enter a positive integer 0 Invalid entry Enter a positive integer: -1 Invalid entry Enter a positive integer: 4 Fibonacci Enter on call 1 to compute Fibonacci 4 Fibonacci Enter on call 2 to compute Fibonacci 3 Fibonacci Enter on call 3 to compute Fibonacci 2 Fibonacci Enter on call 4 to compute Fibonacci 1 Fibonacci Enter on call 5 to compute Fibonacci 0 Fibonacci Enter on call 6 to compute Fibonacci 1 Fibonacci Enter on call 7 to compute Fibonacci 2 Fibonacci Enter on call 8 to compute Fibonacci 1) Fibonacci Enter on call 9 to compute Fibonacci 0 Fibonacci number 4 is 3 Lab 7 Searching and Sorting, Recursion ECE 114Explanation / Answer
#include <iostream>
using namespace std;
int call = 1;
int Fibonacci(int n)
{
if(n<=0 && call == 1)
{
cout<<"Invalid entry ";
return -1;
}
cout<<"Fibonacci: Enter on call "<<call<<" to compute Fibonacci ( "<<n<<" ) ";
call += 1;
if(n==0)
return 0;
if(n == 1)
return 1;
return Fibonacci(n-1) + Fibonacci(n-2);
}
int main()
{
Fibonacci(0);
Fibonacci(-1);
cout<<"Fibonacci number 4 is "<< Fibonacci(4)<<endl;
return 1;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.