write a recursive function definition for a function that has one parameter n of type int and that returns the nth Fibonacci number.

write a recursive function definition for a function that has one parameter n of type int and that returns the nth Fibonacci number.

LANGUAGE: C++

CHALLENGE:

write a recursive function definition for a function that has one parameter n of type int and that returns the nth Fibonacci number.

SOLUTION:



#include <iostream>
using namespace std;

int fib(int n);

int main(){
	int fibNum;

    cout << "Enter the Fibonacci Number you would like to retrieve: ";
    cin >> fibNum;

	//request and print fib num
    cout << "The " << fibNum << " Fibonacci Number is " << fib(fibNum) << "\n";

    system("Pause");

    return 0;

}

int fib(int n){
   if (n <= 1)
      return n;
   return fib(n-1) + fib(n-2);
}

Add comment