Instructions The program in the Programming Example: Fibonacci Number does not check: Whether the first number entered by the user is less than or equal to the second number and whether both the numbers are nonnegative. Whether the user entered a valid value for the position of the desired number in the Fibonacci sequence. Rewrite that program so that it checks for these things. NOTES: If an invalid number is entered for case 1 above, prompt the user to enter both numbers again. If an invalid number is entered for case 2, prompt the user to enter a value until a valid value is entered. the code: #include using namespace std; int main() { //Declare variables int previous1; int previous2; int current; int counter; int nthFibonacci; cout << "Enter the first two Fibonacci numbers: "; //Step 1 cin >> previous1 >> previous2; //Step 2 cout << endl; //verify the first two values if(previous1<0 && previous2>0) cout<<"Enter positive value for the first number\n"; else if (previous1>0 && previous2<0) cout<<"Enter positive value for the second number\n"; else if (previous1<0 && previous2<0) cout<<"Enter positive values\n"; else if (previous1>=previous2) cout<<"The first number should be greater than the second number.\n"; else { cout << "The first two Fibonacci numbers are " << previous1 << " and " << previous2 << endl; //Step 3 cout << "Enter the position of the desired Fibonacci number: " ; //Step 4 cin >> nthFibonacci; //Step 5 cout << endl; //verify the position if(nthFibonacci<1) cout<<"Enter a valid position."; else { if (nthFibonacci == 1) //Step 6.a current = previous1; else if (nthFibonacci == 2) //Step 6.b current = previous2; else //Step 6.c { counter = 3; //Steps 6.c.2 – 6.c.5 while (counter <= nthFibonacci) { current = previous2 + previous1; previous1 = previous2; previous2 = current; counter++; }//end while }//end else /* Output the Fibonacci number at nth position */ cout<

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 11PE: The program in the Programming Example: Fibonacci Number does not check whether the first number...
icon
Related questions
Question
100%

Instructions

The program in the Programming Example: Fibonacci Number does not check:

  1. Whether the first number entered by the user is less than or equal to the second number and whether both the numbers are nonnegative.

  2. Whether the user entered a valid value for the position of the desired number in the Fibonacci sequence.

Rewrite that program so that it checks for these things.

NOTES:

  • If an invalid number is entered for case 1 above, prompt the user to enter both numbers again.

  • If an invalid number is entered for case 2, prompt the user to enter a value until a valid value is entered.

the code: 

#include <iostream>

using namespace std;

int main()

{

//Declare variables

int previous1;
int previous2;
int current;
int counter;
int nthFibonacci;

cout << "Enter the first two Fibonacci numbers: "; //Step 1

cin >> previous1 >> previous2; //Step 2

cout << endl;

//verify the first two values
if(previous1<0 && previous2>0)
cout<<"Enter positive value for the first number\n";
else if (previous1>0 && previous2<0)
cout<<"Enter positive value for the second number\n";
else if (previous1<0 && previous2<0)
cout<<"Enter positive values\n";
else if (previous1>=previous2)
cout<<"The first number should be greater than the second number.\n";
else
{
cout << "The first two Fibonacci numbers are " << previous1 << " and " << previous2 << endl; //Step 3

cout << "Enter the position of the desired Fibonacci number: " ; //Step 4

cin >> nthFibonacci; //Step 5

cout << endl;

//verify the position
if(nthFibonacci<1)
cout<<"Enter a valid position.";
else
{
if (nthFibonacci == 1) //Step 6.a

current = previous1;

else if (nthFibonacci == 2) //Step 6.b

current = previous2;

else //Step 6.c

{

counter = 3;
//Steps 6.c.2 – 6.c.5

while (counter <= nthFibonacci)
{
current = previous2 + previous1;
previous1 = previous2;
previous2 = current;
counter++;

}//end while

}//end else

/* Output the Fibonacci number at nth position */
cout<<current;
}//end else
}

return 0;

}//end main
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Mathematical functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning