Exercise Three:
In this exercise, you will create a program that gets two numbers from the user and checks to make sure the first number is less than the second number. It should then display the sum of the even numbers between, and including, the two numbers entered by the user. In other words, if the user enters an even number, that number should be included in the sum. For example, if the user enters the integers 2 and 7, the sum is 12 (2 + 4 + 6). If the user enters the integers 2 and 8, the sum is 20 (2 + 4 + 6 + 8 ). Display an error message if the first integer entered by the user is greater than the second integer.
- Complete the IPO chart below using the flowchart as a guide. Hint: You’ll need to use the modulus operator for this exercise.
- Double check your code to make sure it matches the flowchart accurately. For example, if flowchart says “while num1 >= num2”, make sure your code has a while loop with a condition of “num1 >= num2”.
- Desk-check the algorithm using 1 and 5 for the first check, 12 and 20 for the second check and 50 and 3 for the third check.
- Code the algorithm into a program called yourlastname_U4W1Exer3. Also enter appropriate comments and any additional instructions required by the compiler.
- Save and run the program. Test the program using the same data used to desk-check the algorithm. Correct any errors.
Here's what I've done:
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int num1 = 0;
int num2 = 0;
int workNum = 0;
int sum = 0;
cout << "Enter num1: ";
cin >> num1;
cout << "Enter num2: ";
cin >> num2;
while (num1 >= num2);
{
cout << "Num1 must be < Num2" << endl;
cout << "Enter num1: ";
cin >> num1;
cout << "Enter num2: ";
cin >> num2;
workNum = num1;
}
if (workNum % 2 == 0);
{
workNum += 1;
}
while (workNum <= num2);
{
sum = num1 + num2;
workNum += 2;
cout << "Sum: " << sum << endl;
}
return 0;
}// end of main fucntion