Algorithms & Flowcharts — Worked Examples
6. Counting & Accumulating with Loops
Counting & Accumulating with Loops
Many programs work by repeating a small step many times while keeping a running number that grows as they go. That running number is called an accumulator (for adding up) or a counter (for counting). In this chapter we build four classic examples. For every one you will see a flowchart, pseudocode, a hand trace table, and real Python so you can run it yourself.
The key idea to watch in each trace table is the accumulator column: how the running total changes on every single pass through the loop.
Example (a): Sum of the numbers 1 to N
Problem
Ask the user for a whole number N and add up every number from 1 up to N (so if N is 5 the answer is 1+2+3+4+5 = 15).
Pseudocode
INPUT N
SET total = 0
SET i = 1
WHILE i <= N
SET total = total + i
SET i = i + 1
OUTPUT total
Trace table
Sample input: N = 5. One row per loop iteration. Watch the total (accumulator) column grow.
| Iteration | i (before check) | i <= 5 ? | total = total + i | total after | i after |
|---|---|---|---|---|---|
| start | 1 | - | - | 0 | 1 |
| 1 | 1 | Yes | 0 + 1 | 1 | 2 |
| 2 | 2 | Yes | 1 + 2 | 3 | 3 |
| 3 | 3 | Yes | 3 + 3 | 6 | 4 |
| 4 | 4 | Yes | 6 + 4 | 10 | 5 |
| 5 | 5 | Yes | 10 + 5 | 15 | 6 |
| exit | 6 | No | - | 15 | 6 |
The loop stops when i becomes 6 (because 6 <= 5 is No), and we output 15.
In Python
N = 5
total = 0
i = 1
while i <= N:
total = total + i
i = i + 1
print(total)
# Output:
# 15
Example (b): Factorial of N
Problem
The factorial of N (written N!) multiplies every whole number from 1 up to N. For example 4! = 1 x 2 x 3 x 4 = 24.
Pseudocode
INPUT N
SET fact = 1
SET i = 1
WHILE i <= N
SET fact = fact * i
SET i = i + 1
OUTPUT fact
Trace table
Sample input: N = 4. The accumulator is fact, and it starts at 1 (not 0, because multiplying by 0 would wipe everything out).
| Iteration | i (before check) | i <= 4 ? | fact = fact * i | fact after | i after |
|---|---|---|---|---|---|
| start | 1 | - | - | 1 | 1 |
| 1 | 1 | Yes | 1 * 1 | 1 | 2 |
| 2 | 2 | Yes | 1 * 2 | 2 | 3 |
| 3 | 3 | Yes | 2 * 3 | 6 | 4 |
| 4 | 4 | Yes | 6 * 4 | 24 | 5 |
| exit | 5 | No | - | 24 | 5 |
Answer: 24.
In Python
N = 4
fact = 1
i = 1
while i <= N:
fact = fact * i
i = i + 1
print(fact)
# Output:
# 24
Example (c): Multiplication table of N (1 to 10)
Problem
Print the multiplication table of N from N x 1 up to N x 10.
Pseudocode
INPUT N
FOR i FROM 1 TO 10
SET product = N * i
OUTPUT N, "x", i, "=", product
Trace table
Sample input: N = 7. One row per iteration; the product column is what gets printed each time.
| Iteration | i | product = 7 * i | Line printed |
|---|---|---|---|
| 1 | 1 | 7 | 7 x 1 = 7 |
| 2 | 2 | 14 | 7 x 2 = 14 |
| 3 | 3 | 21 | 7 x 3 = 21 |
| 4 | 4 | 28 | 7 x 4 = 28 |
| 5 | 5 | 35 | 7 x 5 = 35 |
| 6 | 6 | 42 | 7 x 6 = 42 |
| 7 | 7 | 49 | 7 x 7 = 49 |
| 8 | 8 | 56 | 7 x 8 = 56 |
| 9 | 9 | 63 | 7 x 9 = 63 |
| 10 | 10 | 70 | 7 x 10 = 70 |
In Python
N = 7
for i in range(1, 11):
product = N * i
print(N, "x", i, "=", product)
# Output:
# 7 x 1 = 7
# 7 x 2 = 14
# 7 x 3 = 21
# 7 x 4 = 28
# 7 x 5 = 35
# 7 x 6 = 42
# 7 x 7 = 49
# 7 x 8 = 56
# 7 x 9 = 63
# 7 x 10 = 70
Example (d): Average of N entered numbers
Problem
Ask how many numbers (N) the user will enter, read them one by one, add them into a running total, then divide the total by N to get the average.
Pseudocode
INPUT N
SET total = 0
SET count = 1
WHILE count <= N
INPUT num
SET total = total + num
SET count = count + 1
SET avg = total / N
OUTPUT avg
Trace table
Sample input: N = 4, and the four numbers entered are 10, 20, 30, 40. Watch the total accumulator.
| Iteration | count | num entered | total = total + num | total after | count after |
|---|---|---|---|---|---|
| start | 1 | - | - | 0 | 1 |
| 1 | 1 | 10 | 0 + 10 | 10 | 2 |
| 2 | 2 | 20 | 10 + 20 | 30 | 3 |
| 3 | 3 | 30 | 30 + 30 | 60 | 4 |
| 4 | 4 | 40 | 60 + 40 | 100 | 5 |
| exit | 5 | - | avg = 100 / 4 | 100 | 5 |
Average = 100 / 4 = 25.0.
In Python
N = 4
numbers = [10, 20, 30, 40] # pretend these were typed in one by one
total = 0
count = 1
while count <= N:
num = numbers[count - 1]
total = total + num
count = count + 1
avg = total / N
print(avg)
# Output:
# 25.0