Teachers' Portal  /  IGCSE Computer Science 0478  /  Programming  /  Library routines: MOD, DIV, ROUND and RANDOM
Lesson pack · IGCSE Computer Science 0478 · 8.1 · IGCSE

Library routines: MOD, DIV, ROUND and RANDOM

Ready-made routines. Programming languages come with routines that are already written and tested, so you do not have to write them yourself.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Programming
Syllabus reference
Cambridge IGCSE Computer Science 0478, 8.1
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 7 of 11 in Programming; follows Procedures, functions, parameters and scope; leads into Writing maintainable programs
Central model
Fig. 1: DIV counts the full groups; MOD counts what is left over
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • DIV and MOD: In the lab, choose MOD, DIV and ROUND.
  • ROUND: ROUND takes a value (or an expression) and the number of decimal places to round it to.
  • RANDOM numbers: RANDOM() returns a real number from 0 to 1.
Key points to leave on the board
  • DIV(a, b): whole number of times b goes into a.
  • MOD(a, b): the remainder left over. Check: DIV × b + MOD = a.
  • ROUND(x, n): look at the next decimal place; 5 or more rounds up.
  • RANDOM(): 0 to 1; multiply, ROUND to 0 places, then add to set the range.
The core visual model

One picture every idea returns to

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
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Ready-made routines.. Programming languages come with routines that are already written and tested, so you do not have to write them yourself. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: DIV and MODIn the lab, choose MOD, DIV and ROUND. Project tab 5 of the lab (Library routines) and let a student drive it while the class predicts. Model the worked example Splitting a number on the board, one line at a time. Your turn: Convert 250 seconds to minutes and seconds using DIV and MOD.Whole class, then pairs
13 to 21 minTeach and check: ROUNDROUND takes a value (or an expression) and the number of decimal places to round it to. Model the worked example Rounding on the board, one line at a time.Pairs
21 to 30 minTeach and check: RANDOM numbersRANDOM() returns a real number from 0 to 1. Model the worked example A different range on the board, one line at a time.Anticipation / Reaction Guide
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 17 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in RANDOM numbers, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Anticipation / Reaction Guide

Before the lesson, students mark five statements true or false. After the teaching they mark them again and justify any change of mind in one sentence each. The statements come from the examiner traps below.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
MOD gives the remainder, not the decimal part: MOD(7, 2) is 1, not 0.5.DIV(7, 2) is 3, not 3.5.
ROUND returns a new value; it does not change the variable unless you assign the result back, e.g.Price ← ROUND(Price, 2).
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.
Worksheet

Library routines: MOD, DIV, ROUND and RANDOM: worksheet

6 questions, 17 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · recall[2 marks]

State what is returned by the library routines DIV and MOD.

Q2 · calculate[4 marks]

State the value returned by: (a) DIV(29, 4) (b) MOD(29, 4) (c) ROUND(29 / 4, 0) (d) ROUND(12.3456, 2)

Q3 · write[3 marks]

Write pseudocode that inputs a number of seconds and outputs it as minutes and seconds, using DIV and MOD.

Q4 · write[3 marks]

Write a statement that stores a random whole number from 1 to 100 in the variable Target. Explain why your expression cannot give 0 or 101.

Q5 · apply[3 marks]

Write pseudocode that inputs a whole number and outputs "Even" or "Odd".

Q6 · analysis[2 marks]

A student tests whether Number is even with IF Number / 2 = 0. Explain why this does not work and correct it.

Answer key and marking notes
Q1 · 2 marks
  • DIV returns the quotient of a division with the fractional part discarded (a whole number).
  • MOD returns the remainder of the division.
Q2 · 4 marks
  • (a) 7
  • (b) 1
  • (c) 29 / 4 = 7.25, which rounds to 7
  • (d) 12.35
Q3 · 3 marks
  • INPUT Seconds
  • Mins ← DIV(Seconds, 60) and Secs ← MOD(Seconds, 60)
  • OUTPUT Mins, " minutes ", Secs, " seconds"
Q4 · 3 marks
  • Target ← ROUND(RANDOM() * 99, 0) + 1
  • ROUND(RANDOM() * 99, 0) gives a whole number from 0 to 99.
  • Adding 1 gives 1 to 100, so 0 and 101 are impossible.
Q5 · 3 marks
  • INPUT Number
  • IF MOD(Number, 2) = 0
  • THEN OUTPUT "Even" ELSE OUTPUT "Odd" with ENDIF
Q6 · 2 marks
  • Number / 2 is the result of the division, which is only 0 when Number is 0; it is not the remainder.
  • Correct: IF MOD(Number, 2) = 0.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

What are the values of DIV(29, 6) and MOD(29, 6)?

  • A4 and 5
  • B5 and 4
  • C4.83 and 5
  • D4 and 0.83
Q21 mark

What value is returned by ROUND(7.4567, 2)?

  • A7.46
  • B7.45
  • C7.5
  • D7.457
Q31 mark

RANDOM() returns a random number from 0 to 1 inclusive. Which expression simulates a dice roll, a whole number from 1 to 6?

  • AROUND(RANDOM() * 5, 0) + 1
  • BROUND(RANDOM() * 6, 0)
  • CROUND(RANDOM() * 6, 0) + 1
  • DRANDOM() * 6
Q41 mark

Seconds holds 500. What is output by OUTPUT DIV(Seconds, 60), " min ", MOD(Seconds, 60), " s"?

  • A8 min 20 s
  • B8 min 0.33 s
  • C8.33 min 20 s
  • D20 min 8 s
Q51 mark

What is output?

Number ← 4072
Sum ← 0
WHILE Number > 0 DO
    Sum ← Sum + MOD(Number, 10)
    Number ← DIV(Number, 10)
ENDWHILE
OUTPUT Sum

  • A13
  • B451
  • C4
  • D2
Q61 mark

Which condition is TRUE exactly when Year is divisible by 4 but not divisible by 100?

  • AMOD(Year, 4) = 0 AND MOD(Year, 100) <> 0
  • BMOD(Year, 4) = 0 OR MOD(Year, 100) <> 0
  • CDIV(Year, 4) = 0 AND DIV(Year, 100) <> 0
  • DMOD(Year, 4) <> 0 AND MOD(Year, 100) = 0
Quiz key
Q1: A

6 goes into 29 four whole times (24), leaving 5, so DIV(29, 6) = 4 and MOD(29, 6) = 5. 5 and 4 swaps the two routines. 4.83 is 29 / 6, but DIV discards the fractional part. 0.83 is the decimal part of 29 / 6, not the remainder.

Q2: A

Two decimal places keeps 7.45; the next digit is 6, which is 5 or more, so it rounds up to 7.46. 7.45 just cuts the number off (truncates). 7.5 rounds to one decimal place. 7.457 rounds to three decimal places.

Q3: A

RANDOM() * 5 runs from 0 to 5; rounding gives a whole number 0 to 5, and adding 1 gives 1 to 6. ROUND(RANDOM() * 6, 0) gives 0 to 6, seven values including 0. Adding 1 to that gives 1 to 7. RANDOM() * 6 is not rounded, so it gives values such as 3.72.

Q4: A

60 goes into 500 eight whole times (480) with 20 left over, so the output is 8 min 20 s. 0.33 is the decimal part of 500 / 60, not the remainder. 8.33 is 500 / 60, but DIV gives only the whole-number part. 20 min 8 s swaps the two results.

Q5: A

Each pass adds the last digit and then removes it: 2, then 7, then 0, then 4, giving 2 + 7 + 0 + 4 = 13; then Number is 0 and the loop stops. 451 swaps MOD and DIV (407 + 40 + 4). 4 is the number of passes. 2 is only the first digit added, as if the loop ran once.

Q6: A

MOD(Year, 4) = 0 means divisible by 4, and MOD(Year, 100) <> 0 means not divisible by 100; both must hold, so AND is needed (2024 TRUE, 1900 FALSE). With OR, 2023 is accepted because it is not divisible by 100. DIV gives the quotient, not the remainder, so DIV(Year, 4) = 0 is FALSE for every year from 4 upwards. The last option reverses both tests.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonProcedures, functions, parameters and scope