Arithmetic operations
Programming languages support the standard arithmetic operations and a few extra integer-specific ones that are useful in algorithms.
The basic operations
| Operation | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 7 + 4 | 11 |
| Subtraction | − | 7 − 4 | 3 |
| Multiplication | × | 7 × 4 | 28 |
| Real division | / | 7 / 2 | 3.5 |
These work on integers and reals. Real division returns a real number even when both operands are integers.
Integer division (DIV) and modulus (MOD)
Two extras are essential at GCSE.
- DIV gives the integer quotient of a division (the whole-number part, throwing away the remainder).
- MOD gives the remainder after integer division.
Examples:
| Expression | DIV result | MOD result |
|---|---|---|
| 17 / 5 | 3 | 2 |
| 20 / 4 | 5 | 0 |
| 23 / 7 | 3 | 2 |
| 100 / 25 | 4 | 0 |
In pseudocode: 17 DIV 5 = 3, 17 MOD 5 = 2.
Why DIV and MOD matter
A surprising amount of useful logic uses them:
- Even or odd:
n MOD 2 = 0is true if n is even. - Divisibility:
n MOD 7 = 0is true if n is a multiple of 7. - Last digit:
n MOD 10returns the units digit of n. - Without last digit:
n DIV 10removes the units digit. - Convert seconds to mm:ss: total seconds →
mins = total DIV 60,secs = total MOD 60. - Hour into 12-hour clock:
((h - 1) MOD 12) + 1.
Exponentiation
In AQA pseudocode, exponentiation is shown as x^y or x ** y depending on context.
SET areaCircle TO 3.14159 * radius^2
Most languages allow pow(x, y) as an alternative.
Operator precedence
Standard maths precedence applies (BIDMAS / BODMAS). When in doubt, use brackets:
SET result TO 2 + 3 * 4 -- 14, not 20
SET result TO (2 + 3) * 4 -- 20
SET result TO (a + b) DIV 2 -- average of a, b for integers
Real-vs-integer arithmetic
Be careful when mixing types. 10 / 4 returns 2.5 in real division but 10 DIV 4 returns 2. If your variable is an integer, real division might require explicit conversion.
SET avg TO (a + b) / 2 -- real
SET avg_int TO (a + b) DIV 2 -- integer (rounds down)
✦Worked example— Worked examples
Example 1. Convert 200 seconds to minutes and seconds.
- mins = 200 DIV 60 = 3
- secs = 200 MOD 60 = 20
- So 200 s = 3 min 20 s.
Example 2. Last 3 digits of an integer n.
- n MOD 1000
Example 3. Test whether year is a leap year (simplified rule: divisible by 4):
- IF year MOD 4 = 0 THEN ...
⚠Common mistakes— Pitfalls
- Confusing DIV and MOD. DIV = quotient, MOD = remainder.
- Using / for integer division — gives a real result.
- Forgetting BIDMAS —
a + b * cis not(a + b) * c. - Negative MOD:
-17 MOD 5may give 3 or -2 depending on language. Best avoided in GCSE problems. - Floating-point precision:
0.1 + 0.2is not exactly 0.3 in real arithmetic — small rounding errors. (More relevant at A-Level.)
➜Try this— Quick check
A program needs to find the units digit of a 4-digit number n. Which operation gives it? n MOD 10.
To extract the tens digit? (n DIV 10) MOD 10.
To extract the hundreds digit? (n DIV 100) MOD 10.
AI-generated · claude-opus-4-7 · v3-deep-computer-science