9618/21/O/N/25 question 6: worked solution (October/November 2025, paper 21)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 11.1 Programming Basics Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3. Q33 9618/21/O/N/25 original Q6 Structured · 9 marks Worked solution (a) The check digit is the sum of the digits mod 10. In 41623 the first four digits add up to 13. Any first two digits that add up to 5 (or 15) with 6 and 2 after them also give 13 (or 23), so the check digit is still 3. Number: any number where the first two digits add up to 5 or 15 and the last three are 623, for example 23623 . [1] Explanation: the sum of the digits (13) is the same , so the remainder / check digit is still 3. [1] (b) Turn the number into a string, add up its digits in a loop, take the sum MOD 10, then put the check digit on the end. FUNCTION Generate(Number : INTEGER) RETURNS INTEGER DECLARE Total, Index, CheckDigit : INTEGER DECLARE NumString : STRING Total ← 0 NumString ← NUM_TO_STR(Number) FOR Index ← 1 TO LENGTH(NumString) Total ← Total + STR_TO_NUM(MID(NumString, Index, 1)) NEXT Index CheckDigit ← Total MOD 10 Number ← Number * 10 + CheckDigit RETURN Number ENDFUNCTION Function heading, parameter, return type and ending [1] Total set to 0 before the loop [1] Number converted to a string and a loop for the length of the string [1] Attempt to add up the digits in a loop [1] Digits added up completely correctly [1] CheckDigit worked out as Total MOD 10 [1] CheckDigit added to the end (Number * 10 + CheckDigit) and returned [1] Another way: no string, use a WHILE loop with MOD 10 and DIV 10 to take off each digit and add it to Total. KEY IDEA Add up the digits with a loop, take the sum MOD 10, and put that digit on the end by multiplying by 10 and adding it. TOTAL 9 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by CSFeedbySirNisarAli. Tap the picture to open it on YouTube.