The algorithm below is written in pseudo-code.
n ← 2026
total ← 0
WHILE n > 0
total ← total + (n MOD 10)
n ← n DIV 10
ENDWHILE
OUTPUT total(a) Complete a trace table, with columns n and total, for the algorithm. Show the initial values in the first row. [3]
(b) State the purpose of the algorithm. [1]
- (a) n column correct: 2026, 202, 20, 2, 0(1)
- (a) total column correct up to and including 8: 0, 6, 8, 8(1)
- (a) Final value of total is 10 (and no extra rows); ignore an OUTPUT column if 10 is shown(1)
- (b) It calculates/outputs the sum of the digits of n (the starting number); do not accept 'adds up n'(1)
- Answer: (a) n: 2026, 202, 20, 2, 0; total: 0, 6, 8, 8, 10. (b) Outputs the sum of the digits of the number.
- Examiner tip: MOD 10 gives the last digit and DIV 10 removes it – students who use ordinary division get 202.6 and lose every later row.