Cambridge AS Level Computer Science 9618 · 11.1 Programming Basics

Q33 · 9618/21/O/N/25 Q6

Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3.

Q339618/21/O/N/25original Q6Structured · 9 marks
Question 6 from 9618/21/O/N/25

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 IDEAAdd 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

Mark scheme for 9618/21/O/N/25 question 6
VIDEO
▶ Whole paper

Video solution of the whole paper · by CSFeedbySirNisarAli. 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.