Cambridge IGCSE · Topic 8.1
IGCSE Computer Science 0478 / Topic 8 / IGCSE

Ready-made routines.

Programming languages come with routines that are already written and tested, so you do not have to write them yourself. The 0478 syllabus names four: MOD, DIV, ROUND and RANDOM. Learn exactly what each one returns and the classic jobs they do.

The key idea

A library routine is a pre-written, pre-tested subroutine that comes with a programming language. DIV(a, b) returns the whole-number quotient of a divided by b, with the fractional part discarded. MOD(a, b) returns the remainder. ROUND(x, places) returns x rounded to that number of decimal places. RANDOM() returns a random number between 0 and 1 inclusive.

17 items shared into groups of 5 3 full groups 2 left over DIV(17, 5) = 3MOD(17, 5) = 2 17 ÷ 5 = 3 remainder 2, because 3 × 5 + 2 = 17
Fig. 1: DIV counts the full groups; MOD counts what is left over
Section 01

DIV and MOD.

In the lab, choose MOD, DIV and ROUND. It turns a number of minutes into hours and minutes; try 135, then other values.

DECLARE Minutes : INTEGER
INPUT Minutes
OUTPUT DIV(Minutes, 60), " hours and ", MOD(Minutes, 60), " minutes"

With the input 135: 135 = 2 × 60 + 15, so the output is 2 hours and 15 minutes.

The same idea in Python (for comparison only: the exam uses pseudocode)

minutes = int(input())
print(minutes // 60, "hours and", minutes % 60, "minutes")   # // is DIV, % is MOD
Worked example Splitting a number
Evaluate DIV(472, 100), MOD(472, 10) and MOD(DIV(472, 10), 10).
  1. 472 ÷ 100 = 4 remainder 72, so DIV gives 4.
  2. 472 ÷ 10 = 47 remainder 2, so MOD gives 2.
  3. DIV(472, 10) = 47, then MOD(47, 10) = 7.
Answer: 4, 2 and 7: the hundreds, units and tens digits
Your turn
Convert 250 seconds to minutes and seconds using DIV and MOD.
Examiner trap

MOD gives the remainder, not the decimal part: MOD(7, 2) is 1, not 0.5. DIV(7, 2) is 3, not 3.5.

Section 02

ROUND.

ROUND takes a value (or an expression) and the number of decimal places to round it to.

DECLARE Price : REAL
DECLARE Total : REAL
Price ← 2.499
Total ← ROUND(Price * 3, 2)
OUTPUT Total

2.499 × 3 = 7.497, which rounds to 7.50 at 2 decimal places, so the output is 7.5.

The same idea in Python (for comparison only: the exam uses pseudocode)

price = 2.499
total = round(price * 3, 2)
print(total)    # 7.5
Worked example Rounding
Evaluate ROUND(3.14159, 3) and ROUND(18.5 / 4, 1).
  1. 3.14159 to 3 decimal places: the 4th decimal is 5, so round up to 3.142.
  2. 18.5 / 4 = 4.625.
  3. 4.625 to 1 decimal place: the 2nd decimal is 2, so round down to 4.6.
Answer: 3.142 and 4.6
Examiner trap

ROUND returns a new value; it does not change the variable unless you assign the result back, e.g. Price ← ROUND(Price, 2).

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What does MOD(17, 5) return?

Quick check+10 XP

What does DIV(17, 5) return?

Quick check+10 XP

In the lab's MOD, DIV and ROUND program, the input is 135. Which line is output first?

Quick check+10 XP

Which expression gives a random whole number from 1 to 6?

Section 03

RANDOM numbers.

RANDOM() returns a real number from 0 to 1. Multiply it to stretch the range, round it to get a whole number, and add to shift the range up. The lab's RANDOM program rolls a dice five times.

DECLARE Roll : INTEGER
DECLARE Count : INTEGER
FOR Count ← 1 TO 5
    Roll ← ROUND(RANDOM() * 5, 0) + 1    // a whole number from 1 to 6
    OUTPUT "Roll ", Count, ": ", Roll
NEXT Count
ExpressionSmallestLargest
RANDOM()01
RANDOM() * 505
ROUND(RANDOM() * 5, 0)05 (whole numbers)
ROUND(RANDOM() * 5, 0) + 116

The same idea in Python (for comparison only: the exam uses pseudocode)

import random
roll = random.randint(1, 6)    # Python's library routine for a whole number 1 to 6
Worked example A different range
Write an expression for a random whole number from 1 to 10.
  1. There are 10 values, so the rounded part must go from 0 to 9: ROUND(RANDOM() * 9, 0).
  2. Add 1 to shift 0 to 9 up to 1 to 10.
Answer: ROUND(RANDOM() * 9, 0) + 1
Examiner trap

ROUND(RANDOM() * 6, 0) gives 0 to 6, which is seven possible values, not a dice roll. Check the smallest and largest values of your expression.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which condition is TRUE when Number is even?

Exam style+20 XP

What is the value of DIV(100, 7) + MOD(100, 7)?

Exam style+20 XP

What does ROUND(2.0 / 3.0, 2) return?

Exam style+20 XP

What is a library routine?

Skill unlocked

Library routines, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
DIV, MOD, ROUND and RANDOM: evaluating them and using them to solve problems.