Puzzle L8

Add up even, odd and all integers from 0 to N

[E-12]Write a main() program that first asks the user for an integer, N, and then calculates three sums: the sum of the even integers, the sum of the odd integers, and the sum of all integers from 0 to N. The output of the program is:

Enter n: 7
Sum = 28, Sum of Odd = 16, Sum of Even = 12

Use the previous program as a start for this one. There are several ways to write this program. Try to write a nice, conceptually clean program.

The best way to write this program is to use the formulas that calculate each sum in one step. But, to practice programming, implement this program with a loop.



Previous Page        Answer         Next Page         Home