Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.
Make the first number, then keep making the second one until it is different. Check both are negative before making the third.
DECLARE FirstNumber : INTEGER
DECLARE SecondNumber : INTEGER
DECLARE ThirdNumber : INTEGER
FirstNumber ← INT(RAND(21)) - 10
OUTPUT FirstNumber
REPEAT
SecondNumber ← INT(RAND(21)) - 10
UNTIL FirstNumber <> SecondNumber
OUTPUT SecondNumber
IF FirstNumber < 0 AND SecondNumber < 0 THEN
ThirdNumber ← INT(RAND(6)) + 30
OUTPUT ThirdNumber
ENDIFAll variables declared [1]
RAND() used [1]
INT() used [1]
Conditional loop until two different numbers are generated [1]
INT(RAND(21)) - 10 gives a number from -10 to 10 inclusive [1]
Both different random integers output [1]
Test that both numbers are negative [1]
If so, a third random integer from 30 to 35 inclusive is generated and output [1]
▶ Whole paper
Video solution of the whole paper · by Awab Aqib. 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.