Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
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
Write a recursive void function that has one parameter that is a positive integer.  When called, the function writes its argument to the screen backward.  That is,, if the argument is 1234, it outputs the following to the screen: 4321.   I've literally tried to create this program and I have no idea how to make it work.  Please help me.  Is it possible to explain line by line what the code does?
Create a program that takes two parameters called base and expo such that b ase is not zero and returns the value of baseexpo . Write a recursive function and call it power.
Please can be handwritten. Question 2: Implementing a Recursive Function . Write recursive function, recursionprob(n), which takes a positive number as its argument and returns the output as shown below. The solution should clearly write the steps as shown in an example in slide number 59 and slide number 60 in lecture slides. After writing the steps, trace the function for “recursiveprob(5)” as shown in an example slide number 61. Function Output: >> recursionprob(1) 1 >> recursionprob(2) 1 4 >> recursionprob(3) 1 4 9 >>recrusionprob(4) 1 4 9 16
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