COSC1315 Exam 2 Lab Project

Create a C++ project that asks the user for a series of simple integer numbers. The input loop is to stop when the user enters a negative number.

Set up a single variable named “sum_evens” before you enter the input loop. At the end of the input loop, you should print out the sum of the even numbers.

For each number input, send that number to a module named “check_even” with the number as a parameter. The module should return either the number it received if the number is even, or zero if it is odd. Once you get the result back from the module, add that result to the “sum_evens” variable in the input loop. Here is an example run:

  • Users inputs 1, 2, 3, 4, 5, 6, 7, 8, -1 (no commas, just one number at a time)
  • The program prints out 20.

How do we tell if a number is even or odd?

There are a number of ways to do this. Here is one:

  • Use Integer math:

    • Divide the number by two using integer division.
    • Multiply the result by 2
    • Compare what you get after that last step to the original number. If they are the same, the number was even, if not, it was odd!
  • Look up the “mod” operator “%” in C++

What to turn in

This is due by midnight Sunday, October 25 on Blackboard. Anything submitted after that time will not be graded.