Member Avatar for nana_1

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.

  1. Complete the IPO chart below using the flowchart as a guide. Hint: You’ll need to use the modulus operator for this exercise.
  2. 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”.
  3. 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.
  4. Code the algorithm into a program called yourlastname_U4W1Exer3. Also enter appropriate comments and any additional instructions required by the compiler.
  5. 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

Recommended Answers

All 2 Replies

A couple of areas don't follow the flowchart:

if (workNum % 2 == 0);
{
    workNum += 1;
}

This checks if the number is even not odd. Try if(workNum % 2 == 1)

sum = num1 + num2;

Here you keep adding num1 to num2, but you want to add workNum to the sum. sum += workNum;

Member Avatar for nana_1

@tinstaafl :
Thank you good sir, I did exactly so yet, this is what I get and yes, I did hit enter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.