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 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.
What is the return value of the function call Comp(4, 5), given the recursive function Comp defined below:     int Comp(int a,  int b)  {            if (a + b <= 6)                  return 3;            else                  return Comp(a-1, b) + Comp(a, b-1);     }
In Kotlin, Write and test a recursive function called harm with an expression body that takes an Int argument n and calculates the sum of 1/1 ... 1/n.  For example, harm(3) should return about 1.833333333. You do not need to worry about integer division.
Knowledge Booster
Background pattern image
Similar 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