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
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?
Write a recursive function, reverseDigits, that takes an integer as a parameter and returns the number with the digits reversed. Also, write a program to test your function.
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.
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