Learn
8.1.4F.1 | ARITHMETIC OPERATORS, MOD AND DIV
01 | A PROGRAM CALCULATES WITH EXPRESSIONS
A school sells tickets, packs equipment into boxes and converts a journey duration into hours and minutes. These tasks need calculations, but they do not all need the same kind of division result.
An arithmetic operator specifies a numerical operation, such as addition or multiplication. An expression combines values, variables and operations to produce a result.
In TotalCost ← Quantity * Price, multiplication produces the cost. Assignment then stores that result in TotalCost. The operator and the assignment have separate jobs.
This lesson starts with the four basic operators, then explains the integer quotient and remainder through complete groups and leftover items.
02 | THE FOUR BASIC ARITHMETIC OPERATORS
| Operator | Operation | Example | Result |
|---|---|---|---|
| + | Addition | 7 + 3 | 10 |
| - | Subtraction | 7 - 3 | 4 |
| * | Multiplication | 7 * 3 | 21 |
| / | Division | 7 / 2 | 3.5 |
The values an operator acts on are its operands. In seven plus three, seven and three are the operands.
Subtraction can produce a negative result: seven minus ten is minus three. Division can produce a fractional result even when the supplied values are whole numbers.
Choose suitable data types. A count can use INTEGER; a calculated result that may have a fractional part normally needs REAL in these examples.
03 | USE CURRENT VARIABLE VALUES
Operators can use variables rather than only written literals.
DECLARE Quantity : INTEGER DECLARE Price : REAL DECLARE TotalCost : REAL Quantity ← 3 Price ← 4.50 TotalCost ← Quantity * Price OUTPUT TotalCost
The expression reads three and four point five, multiplies them to thirteen point five, and assigns that result. It does not multiply the words Quantity and Price.
Changing Quantity later does not automatically recalculate TotalCost. Execute the calculation again if the task requires an updated cost.
For a running total, Total ← Total + Amount reads the previous total, adds the current amount and stores the new sum. Keep the right-hand calculation distinct from the destination update.
04 | MULTIPLICATION AND DIVISION COME BEFORE ADDITION
Precedence is the order in which operations within an expression are evaluated. Multiplication and division have higher precedence than addition and subtraction.
| Expression | Working | Result |
|---|---|---|
| 2 + 3 * 4 | Multiply 3 by 4, then add 2 | 14 |
| 20 - 12 / 3 | Divide 12 by 3, then subtract from 20 | 16 |
| (2 + 3) * 4 | Add inside parentheses, then multiply | 20 |
| (20 - 12) / 4 | Subtract inside parentheses, then divide | 2 |
Do not simply calculate every operation from left to right while ignoring precedence. Two plus three times four is not five times four unless parentheses specify that grouping.
Cambridge recommends parentheses to make complex expressions clear. For a mean of two values, write (First + Second) / 2 rather than First + Second / 2.
Precedence within one expression differs from the execution order of separate assignment statements. Both kinds of order must be correct.
05 | ORDINARY DIVISION ANSWERS A NUMERIC QUESTION
Seventeen divided by five is three point four. That result can be useful, but it is not the number of complete five-item groups.
DECLARE Result : REAL Result ← 17 / 5
If seventeen identical items are packed five per full box, you can fill three complete boxes and have two items left. The fractional result three point four describes the ratio, not four complete boxes.
The quotient is the result of division. For integer grouping, we need its whole part and also the remainder, which is what remains after making complete groups.
For the DIV and MOD examples below, use non-negative integer amounts and positive integer group sizes. This keeps the grouping interpretation clear without assuming another language’s rules for negative operands.
06 | DIV RETURNS THE INTEGER QUOTIENT
Cambridge defines DIV using the form DIV(Amount, GroupSize). It returns the quotient with the fractional part discarded.
DECLARE Items : INTEGER DECLARE ItemsPerBox : INTEGER DECLARE FullBoxes : INTEGER Items ← 17 ItemsPerBox ← 5 FullBoxes ← DIV(Items, ItemsPerBox)
FullBoxes becomes three. Seventeen contains three complete groups of five; a fourth complete group would require twenty items.
For these non-negative values, discarding the fractional part is not rounding to the nearest whole number. DIV(19, 5) is three, even though ordinary division gives three point eight.
The two values supplied inside the parentheses are arguments. They must be INTEGER for these Cambridge operations. Use the function-style syntax shown, rather than another language’s // notation.
07 | MOD RETURNS THE REMAINDER
MOD uses the same two integer arguments but answers a different question: how much is left after the complete groups?
DECLARE LeftoverItems : INTEGER LeftoverItems ← MOD(Items, ItemsPerBox)
With Items seventeen and ItemsPerBox five, three groups use fifteen items, leaving two. MOD(17, 5) therefore returns two.
FULL GROUP 1
5 items
FULL GROUP 2
5 items
FULL GROUP 3
5 items
LEFTOVER
2 items
MOD does not give the fractional part point four. The remainder is measured in the original items, so here it is two items.
DIV and MOD complement each other: amount equals integer quotient times group size plus remainder. Seventeen equals three times five plus two.
08 | COMPARE ALL THREE DIVISION RESULTS
| Amount | Group size | Ordinary / result | DIV result | MOD result |
|---|---|---|---|---|
| 17 | 5 | 3.4 | 3 | 2 |
| 19 | 5 | 3.8 | 3 | 4 |
| 20 | 5 | 4 | 4 | 0 |
| 3 | 5 | 0.6 | 0 | 3 |
| 0 | 5 | 0 | 0 | 0 |
An exact division has remainder zero. An amount smaller than the group size has zero complete groups and the whole amount left over.
For these positive divisors, the remainder is at least zero and less than the divisor. A remainder of five after dividing by five would itself form another complete group.
Zero items divided into five-item groups gives zero complete groups and zero remaining items. That is different from trying to divide by a group size of zero.
09 | EXPLORE COMPLETE GROUPS AND LEFTOVERS
Predict the quotient and remainder, then calculate. Try seventeen items in groups of five, twenty in groups of five and three in groups of five.
The illustration shows complete groups separately from leftover items. The demo uses bounded non-negative integer inputs and a positive group size.
10 | COMPLETE BOXES ARE NOT ALWAYS BOXES NEEDED
If every item must be packed, two leftover items still need a partially filled box. For seventeen items with five per box, there are three full boxes but four boxes needed altogether.
FullBoxes ← DIV(Items, ItemsPerBox)
LeftoverItems ← MOD(Items, ItemsPerBox)
BoxesNeeded ← FullBoxes
IF LeftoverItems > 0
THEN
BoxesNeeded ← BoxesNeeded + 1
ENDIFAssume all named items are INTEGER with a positive ItemsPerBox. Add the extra box only if a remainder exists.
For twenty items, DIV is four and MOD is zero, so four boxes are enough. Always adding one would incorrectly request five boxes. For zero items, zero boxes are needed.
Read the scenario’s requested result: “complete groups”, “leftover items” and “containers needed for everything” are three different questions.
11 | CONVERT MINUTES TO HOURS AND REMAINING MINUTES
A journey lasts one hundred thirty-seven minutes. Each hour contains sixty minutes, so DIV counts complete hours and MOD finds minutes not included in those hours.
DECLARE TotalMinutes : INTEGER DECLARE Hours : INTEGER DECLARE RemainingMinutes : INTEGER TotalMinutes ← 137 Hours ← DIV(TotalMinutes, 60) RemainingMinutes ← MOD(TotalMinutes, 60) OUTPUT "Hours: ", Hours OUTPUT "Remaining minutes: ", RemainingMinutes
The outputs are two hours and seventeen remaining minutes, because one hundred thirty-seven equals two times sixty plus seventeen.
Ordinary division gives a fractional number of hours, but its fractional part is not a minute count. Use the original unit with MOD to obtain the remaining minutes.
Test exactly sixty minutes, fewer than sixty and zero to check exact division, no complete hours and empty duration.
12 | MOD HELPS TEST EVENNESS AND EXTRACT DIGITS
An even integer divides by two without a remainder. For non-negative Number, MOD(Number, 2) equal to zero identifies an even value.
IF MOD(Number, 2) = 0
THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
ENDIFAssume Number is declared INTEGER and assigned. Eight gives remainder zero; seven gives remainder one.
Dividing a non-negative decimal integer by ten can also separate its last digit from the earlier digits.
LastDigit ← MOD(Number, 10) EarlierDigits ← DIV(Number, 10)
With Number one thousand two hundred thirty-four, LastDigit is four and EarlierDigits is one hundred twenty-three. This processes a numeric value; it does not preserve the formatting or leading zeros of a text identifier.
13 | CHECK THE DIVISOR BEFORE CALCULATING
The divisor is the number you divide by. Division by zero is undefined, so do not perform /, DIV or MOD with a zero divisor.
IF GroupSize > 0
THEN
FullGroups ← DIV(Amount, GroupSize)
Remainder ← MOD(Amount, GroupSize)
OUTPUT FullGroups, Remainder
ELSE
OUTPUT "Group size must be positive"
ENDIFAssume INTEGER declarations and a non-negative Amount. This grouping scenario requires a positive GroupSize, so its guard also excludes negative sizes.
The check must come before the operations. Calculating first and displaying an error afterwards does not prevent the invalid division.
A clear prompt helps request a positive size, but the prompt does not enforce it without the actual check or an explicit valid-input assumption.
14 | TRACE CALCULATION AND INTERPRETATION
| Instruction with Items = 17 and ItemsPerBox = 5 | Stored result | Meaning |
|---|---|---|
| FullBoxes ← DIV(Items, ItemsPerBox) | 3 | Complete boxes |
| LeftoverItems ← MOD(Items, ItemsPerBox) | 2 | Items not in complete boxes |
| BoxesNeeded ← FullBoxes | 3 | Starting container count |
| If leftovers > 0, add one | 4 | Containers needed for all items |
Trace variables after each assignment. Do not replace DIV’s three with four just because the real task needs an extra partial container; represent that additional rule explicitly.
For arithmetic tests, include an exact multiple, a non-exact multiple, an amount smaller than the divisor and a zero amount. Test invalid divisors if the requirements demand their handling.
For precedence, choose values where parentheses change the answer rather than a coincidental example giving the same result either way.
15 | COMMON MISTAKES AND CLEAR EXPLANATIONS
DIV treated as rounding: it discards the fractional part of the quotient.
MOD treated as the fractional part: it returns the remainder in original units.
Ordinary division used for an item count: distinguish a numeric ratio from complete groups.
One extra container always added: add it only when a remainder exists.
Precedence ignored: multiplication and division come before addition and subtraction; parentheses make grouping clear.
Zero divisor checked too late: guard before calculating.
A useful explanation is: “DIV finds three complete five-item boxes and MOD gives two leftover items. Because those items still need packing, the algorithm adds one partial box, resulting in four boxes needed.”
The next lesson covers relational and logical operators, which produce and combine Boolean conditions.