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).

Start INPUT N total = 0 , i = 1 is i <= N ? decision Yes total = total + i i = i + 1 loop back No OUTPUT total End

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.

Iterationi (before check)i <= 5 ?total = total + itotal afteri after
start1--01
11Yes0 + 112
22Yes1 + 233
33Yes3 + 364
44Yes6 + 4105
55Yes10 + 5156
exit6No-156

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.

Start INPUT N fact = 1 , i = 1 is i <= N ? Yes fact = fact * i i = i + 1 loop back No OUTPUT fact End

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).

Iterationi (before check)i <= 4 ?fact = fact * ifact afteri after
start1--11
11Yes1 * 112
22Yes1 * 223
33Yes2 * 364
44Yes6 * 4245
exit5No-245

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.

Start INPUT N i = 1 is i <= 10 ? Yes OUTPUT N x i = N*i i = i + 1 loop back No OUTPUT done End

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.

Iterationiproduct = 7 * iLine printed
1177 x 1 = 7
22147 x 2 = 14
33217 x 3 = 21
44287 x 4 = 28
55357 x 5 = 35
66427 x 6 = 42
77497 x 7 = 49
88567 x 8 = 56
99637 x 9 = 63
1010707 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.

Start INPUT N total = 0 , count = 1 is count <= N ? Yes INPUT num total = total + num count = count + 1 loop back No avg = total / N OUTPUT avg End

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.

Iterationcountnum enteredtotal = total + numtotal aftercount after
start1--01
11100 + 10102
222010 + 20303
333030 + 30604
444060 + 401005
exit5-avg = 100 / 41005

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