...
/Solution Review: Calculate nth Fibonacci Number Using Recursion
Solution Review: Calculate nth Fibonacci Number Using Recursion
Let's go over the solution review of the challenge given in the previous lesson.
We'll cover the following...
Solution #
Press the RUN button and see the output!
Press + to interact
#include <iostream>using namespace std;// Recursive fibonacci functionint Fibonacci(int n) {// Base Caseif (n == 0) {return 0;}else if (n == 1) {return 1;}// Recursive Caseelse {return Fibonacci(n - 1) + Fibonacci(n - 2);}}// main functionint main() {// Initialize variable nint n = 4;// Declare variable resultint result;// Call fibonacci function in main and store its output in resultresult = Fibonacci(4);// Print value of resultcout << n << "th Fibonacci number = " << result;return 0;}
Explanation
fibonacci function
The recursive fibonacci
function ...