Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 14, Problem 1P

Explanation of Solution

Recursive function for nth Fibonacci numbers:

The recursive function for nth Fibonacci number is shown below:

/* Function definition for compute nth Fibonacci number */

int recursiveFibFunction(int n)

{

       /* If "n" is less than or equal to "1", then */

       if (n <= 1)

              //Return value of "n"

              return n;

  /* Otherwise Recursively call the function "recursiveFibFunction" */

  return recursiveFibFunction(n-1) + recursiveFibFunction(n-2);

}

Explanation:

The above function is used to compute the nth Fibonacci numbers.

  • In this function, first check the value of “n”. If the value of “n” is less than or equal to “1” that is value of “n” is either “1” or “0”, then returns the given “n” value.
  • Otherwise, recursively call the function “recursiveFibFunction”.

Complete executable code:

The complete code is implemented for Fibonacci number is shown below:

//Header file

#include<iostream>

//For standard input and output

using namespace std;

//Function declaration for "recursiveFibFunction" function

int recursiveFibFunction(int n);

//Main function

int main ()

{

       //Initializes the number to "8"

       int number = 8;

  /* Dispay fibonacci number by calling the function "recursiveFibFunction" */

  cout << number << "th Fibonacci number is: "<< recursiveFibFunction(number) << endl;

       return 0;

}

/* Function definition for compute nth Fibonacci number */

int recursiveFibFunction(int n)

{

       /* If "n" is less than or equal to "1", then */

       if (n <= 1)

              //Return value of "n"

              return n;

  /* Otherwise Recursively call the function "recursiveFibFunction" */

  return recursiveFibFunction(n-1) + recursiveFibFunction(n-2);

}

Expert Solution & Answer
Check Mark
Sample Output

8th Fibonacci number is: 21

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Pllleasassseee ssiiirrrr soolveee thissssss questionnnnnnn
Pllleasassseee ssiiirrrr soolveee thissssss questionnnnnnn
Pllleasassseee ssiiirrrr soolveee thissssss questionnnnnnn
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr