Worked solution from the Algorithm design and problem-solving Paper 2 Algorithms, Programming and Logic booklet, Part 6 of 8.
Run the REPEAT loop one input at a time. Answer starts as Value and is multiplied by Count from Value - 1 down to 1. Input -1 ends the algorithm, so 20, 9 and 4 are never read.
| Value | Count | Answer | OUTPUT |
|---|---|---|---|
| 5 | 5 | ||
| 4 | 20 | ||
| 3 | 60 | ||
| 2 | 120 | ||
| 1 | 120 | 120 | |
| 6 | 6 | ||
| 5 | 30 | ||
| 4 | 120 | ||
| 3 | 360 | ||
| 2 | 720 | ||
| 1 | 720 | 720 | |
| -1 |
Value column correct [1]
Count column correct [1]
Answer column correct down to the first OUTPUT (120) [1]
Answer column correct for the remaining rows [1]
OUTPUT column correct [1]
It works out the factorial of the input number: it multiplies the number by each whole number below it until 1 (for example 5 × 4 × 3 × 2 × 1). [1]
The loop would never end properly. Write two of these:
The program would accept the value and enter the FOR loop [1]
Count would keep going down by 1 and never reach 1, because it starts below 1, so there is an endless loop [1]
▶ Topic lesson
A lesson on this topic · by Mr Bulmer's Learning Zone. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.