Absolute C++
Absolute C++
6th Edition
ISBN: 9780133970784
Author: Walter Savitch, Kenrick Mock
Publisher: Addison-Wesley
Question
Book Icon
Chapter 13, Problem 1PP
Program Plan Intro

Program plan:

1 . The following variables are used in the program:

  • seq variable is of integer type and used to store the user-entered Fibonacci sequence.
  • nvariable is of integer type and used as a parameter of the function name findFibonacciNumber.

2. The following functions are used in the program:

  • findFibonacciNumber ()function is used to find the Fibonacci number of the user-entered sequence.
  • main () function is used to get the user input, call the function, and display the Fibonacci number returned by the function.

Program description:

The main purpose of the program is to create a function named findFibonacciNumber . This function get the sequence of the Fibonacci series as parameter, then calculate the Fibonacci number forsequence, and return the Fibonacci number.

Expert Solution & Answer
Check Mark

Explanation of Solution

Program:

//including essential header file
#include <iostream>

//using standard namespace
using namespace std;

//function to find the Fibonacci number of the given sequence
int findFibonacciNumber(int n)
{
//when the fibonacci sequence is equal to 0 or 1
if (n == 0 || n == 1)
    {
//return 1
return 1;
    }

//call the function recursively to find the fibonacci number
return findFibonacciNumber(n-1) + findFibonacciNumber(n-2);
}
//main function
int main() 
{
//variable declaration to store the sequence of Fibonacci number
int seq;
//prompt the user to enter the fibonacci sequence
cout<<"Enter the fibonacci sequence: ";
//get the value from the user
cin>>seq;
//call the function to find and display the fibonacci number of the given sequence
cout<<"The fibonacci number of sequence "<<seq<<" is "<<findFibonacciNumber(seq);
}

Explanation:  

In the above code, a recursive function named findFibonacciNumber is used to find the Fibonacci number of the user-entered sequence. It has a parameter named n of integer type. It gets the sequence of the Fibonacci series from the main () function and stored in the parameter named n. When the value of the Fibonacci sequence is 0 or 1 then this function returns 1. For the other values this function return the Fibonacci number for the given sequence by adding the immediate last two Fibonacci number by calling itself recursivelyand decreasing the value of the parameter by 1 (to get the immediate last value) and 2 (to get the second last value).

In the main function, create a variable named seq of integer type to store the user-entered Fibonacci sequence. Prompt the user to enter the Fibonacci sequence. Store the user-entered Fibonacci sequence in the variable name seq. Call the function named findFibonacciNumber with seq as parameter. Display the function returned value.

Sample output:

Absolute C++, Chapter 13, Problem 1PP , additional homework tip  1

Absolute C++, Chapter 13, Problem 1PP , additional homework tip  2

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
Beeblebrox question help
Consider the following recursive function: if b = 0, if 6 > a > 0, a f(b, a) f (b, 2.(a mod b)) otherwise. f(a, b) = Estimate the number of recursive applications required to compute f(a, b).
Matlab Question

Chapter 13 Solutions

Absolute C++

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