Cambridge Curriculum Section 8.1.7

Library Routines in Pseudocode

Complete mastery of pre-tested library routines: MOD, DIV, ROUND, RANDOM, and string manipulation for Cambridge IGCSE (0478/0984) and O Level (2210).

⏱️ 7 min read 🎯 Paper 2 Essential

⚡ Quick Summary: Cambridge 8.1.7 Library Routines

  • MOD(Number, Divisor): Returns remainder of integer division. MOD(10, 3) → 1.
  • DIV(Number, Divisor): Returns quotient (whole number part) of division. DIV(10, 3) → 3.
  • ROUND(Value, Places): Rounds REAL number to specified decimal places. ROUND(6.97354, 2) → 6.97.
  • RANDOM(): Generates random REAL number between 0 and 1 inclusive. Value ← RANDOM().
  • LENGTH(String): Returns character count. LENGTH("Computer Science") → 16.
  • SUBSTRING(String, Start, Length): 1-indexed slice. SUBSTRING("Computer Science", 10, 7) → "Science".
  • UCASE(String) / LCASE(String): Converts case. UCASE("hello") → "HELLO", LCASE("HELLO") → "hello".

1. What are Library Routines? (Section 8.1.7)

Many programming language development systems include library routines that are ready to incorporate into a program. These routines are fully tested and ready for use.

A programming language IDE usually includes a standard library of functions and procedures. These standard library routines perform many types of everyday tasks — such as mathematical operations, number conversions, and the string handling discussed in Section 8.1.4.

Standard Task Identification

Each programming language has many library routines for standard tasks commonly required by programs. Sometimes a set of routines needs to be specifically identified or imported at the top of a program. For example, in Java, the statement import java.lang.String; is required at the start of a program to provide access to the string handling library. In Python, statements like import math and import random load external libraries.

In Cambridge IGCSE Pseudocode, library routines are built directly into the syllabus — you can call them immediately in expressions or assignment statements without import declarations!

2. Core Math Routines: MOD, DIV, ROUND & RANDOM

You will need to use four core mathematical library routines in your programs for IGCSE Computer Science:

Library Routine Operation Example in Pseudocode Result
MOD Returns remainder of a division Value1 ← MOD(10, 3) 1
DIV Returns quotient (whole number part) Value2 ← DIV(10, 3) 3
ROUND Rounds to a given decimal places Value3 ← ROUND(6.97354, 2) 6.97
RANDOM Returns a random number between 0 and 1 inclusive Value4 ← RANDOM() 0.81...

3. Deep Dive: Division with MOD vs DIV

Standard division (10 / 3) gives a floating-point number (3.3333...). In computer science algorithms, we often need to separate integer division into its quotient and remainder:

MOD (Remainder)

MOD(10, 3) returns 1.

Key Exam Uses:
• Check if number is even: IF MOD(Num, 2) = 0 THEN
• Check if number is a multiple of 5: IF MOD(Num, 5) = 0 THEN
• Extracting cents / pennies: Cents ← MOD(TotalCents, 100)

DIV (Integer Quotient)

DIV(10, 3) returns 3.

Key Exam Uses:
• Minutes to whole hours: Hours ← DIV(TotalMins, 60)
• Dollars from total cents: Dollars ← DIV(TotalCents, 100)
• Binary conversion: repeatedly dividing by 2

Time Converter with DIV and MOD
DECLARE TotalSeconds : INTEGER
DECLARE Minutes : INTEGER
DECLARE SecondsRemaining : INTEGER

TotalSeconds ← 185

// DIV gives the whole minutes (3)
Minutes ← DIV(TotalSeconds, 60)

// MOD gives the remaining seconds (5)
SecondsRemaining ← MOD(TotalSeconds, 60)

OUTPUT Minutes, " minutes and ", SecondsRemaining, " seconds"

4. Rounding & Random Number Generation

Numerical processing in algorithms requires controlled precision and stochastic (random) selection:

ROUND(Value, DecimalPlaces)

Rounds a REAL value to a given number of decimal places.
• ROUND(6.97354, 2) → 6.97
• ROUND(3.14159, 0) → 3
• ROUND(8.756, 1) → 8.8

RANDOM()

Returns a pseudo-random REAL value between 0 and 1 inclusive (e.g. 0.48291...).

Exam Formula: Random Integer between 1 and N
To simulate a standard 6-sided die, combine RANDOM() with INT():
DieRoll ← INT(RANDOM() * 6) + 1

5. String Handling Library Routines (Section 8.1.4)

In Cambridge Paper 2, string operations are formally classified as standard library routines. Always adhere to the official textbook specifications:

1. LENGTH Routine

Length Language Notes
LENGTH("Computer Science")
LENGTH(MyString)
Pseudocode Text in quotes can be used or a variable with data type string.
Returns integer count: 16.

2. SUBSTRING Routine — to extract the word 'Science'

Substring Language Notes
SUBSTRING("Computer Science", 10, 7)
SUBSTRING(MyString, 10, 7)
Pseudocode Text in quotes can be used or a variable with data type string.
• First parameter: is the string.
• Second parameter: is the position of the start character.
• Third parameter: is the length of the required substring.
Pseudocode strings start at position one.

Table 8.11 — Converting a string to upper case

Upper Language Notes
UCASE("Computer Science")
UCASE(MyString)
Pseudocode Text in quotes can be used or a variable with data type string.
Returns: "COMPUTER SCIENCE".

Table 8.12 — Converting a string to lower case

Lower Language Notes
LCASE("Computer Science")
LCASE(MyString)
Pseudocode Text in quotes can be used or a variable with data type string.
Returns: "computer science".
Complete Routine Showcase
DECLARE TextStr : STRING
DECLARE UpperStr : STRING
DECLARE LowerStr : STRING

TextStr ← "Computer Science"

UpperStr ← UCASE(TextStr)   // "COMPUTER SCIENCE"
LowerStr ← LCASE(TextStr)   // "computer science"

OUTPUT "Uppercase: ", UpperStr
OUTPUT "Lowercase: ", LowerStr
OUTPUT "Extracted: ", SUBSTRING(TextStr, 10, 7)
OUTPUT "Length: ", LENGTH(TextStr)

6. Pseudocode vs Python vs Java Comparison

Operation Cambridge Pseudocode 🐍 Python ☕ Java
Remainder MOD(10, 3) 10 % 3 10 % 3
Integer Division DIV(10, 3) 10 // 3 10 / 3 (integers)
Rounding ROUND(Val, 2) round(val, 2) Math.round(val)
Random Number RANDOM() random.random() Math.random()
Length LENGTH(S) len(s) s.length()
Substring SUBSTRING(S, 10, 7) s[9:16] (0-indexed) s.substring(9, 16)
Uppercase UCASE(S) s.upper() s.toUpperCase()
Lowercase LCASE(S) s.lower() s.toLowerCase()

7. Worked Exam Problem & Interactive Runner

Cambridge Exam Style Challenge

Task: An online shopping system asks the user for a product code and a quantity of items ordered. Each box holds 12 items. Write pseudocode that:
1. Inputs the item code and standardizes it in all uppercase letters using UCASE.
2. Calculates how many full boxes are needed using DIV.
3. Calculates how many loose items remain using MOD.
4. Outputs the standardized code, full boxes, and remaining items.

Hover or tap the box to reveal the examiner-approved model answer. Click Run Code to launch it live in the IDE!

Tap to Reveal Answer
DECLARE ProductCode : STRING
DECLARE Quantity : INTEGER
DECLARE FullBoxes : INTEGER
DECLARE LooseItems : INTEGER

OUTPUT "Enter product code:"
INPUT ProductCode

OUTPUT "Enter quantity of items:"
INPUT Quantity

// Standardize code to uppercase
ProductCode ← UCASE(ProductCode)

// Calculate boxes (quotient) and leftover items (remainder)
FullBoxes ← DIV(Quantity, 12)
LooseItems ← MOD(Quantity, 12)

OUTPUT "Product: ", ProductCode
OUTPUT "Full Boxes: ", FullBoxes
OUTPUT "Loose Items: ", LooseItems

8. Common Cambridge Exam Traps & Tips

❌ Trap 1: Confusing MOD and DIV

Remember: DIV is the whole division result (Quotient). MOD is what's left over (Remainder). E.g., DIV(7, 2) = 3, while MOD(7, 2) = 1.

❌ Trap 2: Python 0-Indexing on SUBSTRING

Pseudocode strings are 1-indexed! SUBSTRING("Computer Science", 1, 3) returns "Com". If you write SUBSTRING(S, 0, 3) you lose marks.

❌ Trap 3: SUBSTRING 3rd Parameter is LENGTH, not End Index

In Cambridge pseudocode, the 3rd parameter of SUBSTRING(String, Start, Length) is the number of characters to take, NOT the finishing character index!

✅ Tip 4: Test Any Routine Live in PseudoStudio

PseudoStudio's online compiler includes live syntax highlighting, autocomplete, and deterministic execution for all Cambridge library routines: MOD, DIV, ROUND, RANDOM, LENGTH, SUBSTRING, UCASE, and LCASE.

Ready to Write & Test Algorithms?

Open our free online pseudocode editor with built-in library routine autocomplete and live execution.

Open Online IDE → Solve Practice Questions