go to previous page   go to home page   go to next page


Probably not. Even with an electronic calculator this would be tedious.

Adding Up Integers

There is a formula for the sum of integers from 1 to N, and similar formulas for the sum of even or of odd integers from 1 to N, but pretend you don't know that. Let us write a program that calculates three sums by directly adding up the integers:

The user enters N, and the program counts up from one to N, adding each integer to the appropriate sum. This flowchart that shows how the program works.

flowchart of program

The flowchart shows a loop that increments count from 1 to N. In the loop body, the current value of count is always added to the sum of all integers.

But more work needs to be done. count should be added to the sum of odds only if it is odd, and added to the sum of evens only if it is even. This box: decide which sums should get the number needs more detail.


How can you decide which if count should be added to even or odd?