Puzzle L15


For each integer N from 1 to 25 compute and print the sum 1+2+...+N

Write a main() program that computes and prints N sums: 1, 1+2, 1+2+3, ... , 1+2+3+...+N. There is a smart way to do this with just one loop, and a not-so-smart way that uses nested loops. Try to write the smart way from scratch.

N        Sum
--       ---
1        1
2        3
3        6
4        10
5        15
6        21
7        28
8        36
9        45
10       55
11       66
12       78
13       91
14       105
15       120
16       136
17       153
18       171
19       190
20       210
21       231
22       253
23       276
24       300
25       325 


Previous Page        Answer         Next Page         Home