Rewrite the given program to follow these 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. the program: //************************************************************* // Author: D.S. Malik // // Program: nth Fibonacci number // Given the first two numbers of a Fibonacci sequence, this // determines and outputs the desired number of the Fibonacci // sequence. //************************************************************* #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; 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; 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 */ return 0; }//end main
Operations
In mathematics and computer science, an operation is an event that is carried out to satisfy a given task. Basic operations of a computer system are input, processing, output, storage, and control.
Basic Operators
An operator is a symbol that indicates an operation to be performed. We are familiar with operators in mathematics; operators used in computer programming are—in many ways—similar to mathematical operators.
Division Operator
We all learnt about division—and the division operator—in school. You probably know of both these symbols as representing division:
Modulus Operator
Modulus can be represented either as (mod or modulo) in computing operation. Modulus comes under arithmetic operations. Any number or variable which produces absolute value is modulus functionality. Magnitude of any function is totally changed by modulo operator as it changes even negative value to positive.
Operators
In the realm of programming, operators refer to the symbols that perform some function. They are tasked with instructing the compiler on the type of action that needs to be performed on the values passed as operands. Operators can be used in mathematical formulas and equations. In programming languages like Python, C, and Java, a variety of operators are defined.
Rewrite the given program to follow these instructions:
The program in the
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.
the program:
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 5 images