Write a recursive function definition for a function that has one
parameter n of type int and that returns the n th Fibonacci number.
The Fibonacci numbers are F0 is 1, F1 is 1, F2
is 2, F3 is 3, F4 is 5, and in
general
Fi+2 = Fi + Fi+1 for i = 0, 1, 2, ...
The following recursive function "FibNum" is used to accept an integer parameter and returns the nth Fibonacci number:
//Function definition of fibNum
//This function accepts an intger parameter n
int fibNum(int n)
{
//Find the nth Fibonacci number
//using recursive function
if(n<=1)
return n;
else
//recursive function
return fibNum(n-1)+fibNum(n-2);
}
COMPLETE EXECUTABLE C++ CODE:
//Default header function
#include <iostream>
using namespace std;
//Function prototype
int fibNum(int n);
//Program starts with main method
int main()
{
//Define an intger data type variable name, number
int number;
//Prompt statement for read an integer number
cout<<"Enter an integer number:";
//Read integer number from the student/user/programmer
cin>>number;
//Call the fibNum function and display result on screen
cout<<"The Fibonacci number of F("<<number<<") is "<<fibNum(number);
return 0;
}
//Metho definition of fibNum
//This function accepts one
//intger parameter n
int fibNum(int n)
{
//Find the nth Fibonacci number
//using recursive function
if(n<=1)
return n;
else
//recursive function
return fibNum(n-1)+fibNum(n-2);
}
OUTPUT OF THE PROGRAM CODE: