Question: The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, where the first two terms are 0 and 1, and each term

The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, where the first two terms are 0 and 1, and each term thereafter is the sum of the two preceding terms.

Write a C++ program that repeatedly prompts for and reads a positive integer value n and then calculates and displays the nth number in the Fibonacci sequence (the program should force reentry if the user enters a non-positive n). For example, if n = 8, then the program would display 13.

Program requirements:

  1. Use a do loop to ensure that the user enters a positive value for n.
  2. Use a for loop to perform the calculations needed to determine the nth Fibonacci number.
  3. Use a do loop to control the program repetition for finding more Fibonacci numbers.

Following is a sample output from the program (user input is bold):

Enter a positive integer n: -3

Enter a positive integer n: 0

Enter a positive integer n: 8

8th Finbonacci number 13

Continue (y or n)? y

Enter a positive integer n: 5

5th Finbonacci number 3

Continue (y or n)? n

--------------------------------

Process exited after 37.34 seconds with return value 0

Press any key to continue . . .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!