Worked solution from the Algorithm design and problem-solving Paper 2 Algorithms, Programming and Logic booklet, Part 8 of 8.
Plan: input the number, take 2 characters starting at position 2 (after the '+'), then compare them with the two country codes. One correct answer:
OUTPUT "Please enter a telephone number"
INPUT Telephone
Code ← SUBSTRING(Telephone, 2, 2)
IF Code = "44"
THEN
OUTPUT "UK"
ELSE
IF Code = "20"
THEN
OUTPUT "Egypt"
ELSE
OUTPUT "Unknown"
ENDIF
ENDIFTelephone number input correctly [1]
Use of SUBSTRING (or similar) to take the code [1]
... with the correct start and length, so the 2 digits after the '+' are extracted [1]
IF (or CASE) used to check the code, with matching ENDIF / ENDCASE [1]
Matching country output for 44 and 20, and Unknown output otherwise [1]
Other correct ways also score, for example a CASE OF statement or two separate IF statements.
Write that you need a loop that keeps asking until the length is right:
Find the length of the number entered, using LENGTH(Telephone) [1]
Use a suitable loop, for example REPEAT ... UNTIL or WHILE ... ENDWHILE, with the prompt and INPUT inside it [1]
Use the correct condition: the loop stops when LENGTH(Telephone) = 13 (a WHILE loop repeats while it is not 13) [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.