Unit 3: Programming · Math

Article

Arithmetic and MOD

8 min read

A shop till adds up your prices. A game works out how far your character moved. A clock app wraps from 11 back to 12. All of them use the same small set of operators. An operator is a symbol that does a calculation, like +. This article covers every one the exam uses, including a strange one called MOD.

The arithmetic operators

An expression is a piece of code that the computer works out to get a single value, for example 2 + 3. The values it works on are called operands. Here are the five operators of the exam, next to their Python versions:

OperationAP pseudocodePythonExampleResult
Adda + ba + b17 + 522
Subtracta - ba - b17 - 512
Multiplya * ba * b17 * 585
Dividea / ba / b17 / 53.4
Remaindera MOD ba % b17 MOD 52

In AP pseudocode / is real division, so 17 / 5 is 3.4 and not 3. Run them yourself:

Runs in your browser

Python has one more division, //, which keeps only the whole number: 17 // 5 is 3. It is not in AP pseudocode, so the exam never uses it. Python's / has one more small difference: it always shows a decimal point, so 10 / 2 is 5.0 in Python, while the exam says 10 / 2 is 5. The value is the same.

MOD: the remainder

When you divide 17 sweets between 5 students, each gets 3 and 2 are left over. The leftover is the remainder. The MOD operator gives exactly that:

17 ÷ 5 = 3 remainder 2        so   17 MOD 5 = 2

Read a MOD b as "the remainder when a is divided by b". Try these in your head, then check:

AP pseudocode
DISPLAY(17 MOD 5)
DISPLAY(20 MOD 5)
DISPLAY(3 MOD 5)
Runs in your browser

The program shows 2, then 0, then 3. Look at the last one: 5 goes into 3 zero times, so all of 3 is left over. Here is the pattern for MOD 3. It never reaches 3:

n012345678
n MOD 3012012012

a MOD b is always between 0 and b - 1. On the exam, a is zero or more and b is more than zero. MOD is useful because of the patterns it gives:

UseExpressionWhat it tells you
Even or oddn MOD 20 means even, 1 means odd
Divides exactlyn MOD 5 is 0n is a multiple of 5
Last digitn MOD 1047 MOD 10 is 7
Wrap around a clock(9 + 5) MOD 125 hours after 9 o'clock is 2 o'clock

Check your understanding

What is the value of 29 MOD 7?

Order of operations

What is 2 + 3 * 4? The computer does not simply go from left to right. It follows the rules you know from maths:

  1. Anything in brackets ( ) is worked out first.
  2. Then *, / and MOD. These three are of equal rank.
  3. Then + and -. These two are also of equal rank.
  4. When operators have the same rank, work left to right.
ExpressionHow it is worked outResult
2 + 3 * 43 * 4 is 12 first, then 2 + 1214
(2 + 3) * 4brackets first: 5 * 420
20 - 6 - 2left to right: 14 - 212
10 - 2 * 32 * 3 is 6, then 10 - 64
7 + 10 MOD 410 MOD 4 is 2, then 7 + 29
12 / 4 * 3left to right: 3 * 39

Check your understanding

What is the value of 10 - 2 * 3?

Worked example: sweets and leftovers

A teacher has 17 sweets for 5 students. Find what each student gets if the sweets are shared exactly, and how many are left if every student gets a whole number.

AP pseudocode
sweets ← 17
students ← 5
share ← sweets / students
left ← sweets MOD students
DISPLAY(share)
DISPLAY(left)
Runs in your browser
After this statementsweetsstudentsshareleft
sweets ← 1717
students ← 5175
share ← sweets / students1753.4
left ← sweets MOD students1753.42

The program shows 3.4 and 2. If the sweets must be handed out whole, each student gets 3 and 2 remain. MOD finds the 2.

Now a longer expression: 3 + 12 MOD 5 * 2. Take it one operator at a time:

  1. MOD and * rank higher than +, and they are equal to each other, so go left to right. First 12 MOD 5 is 2. The expression becomes 3 + 2 * 2.
  2. Next 2 * 2 is 4. The expression becomes 3 + 4.
  3. Last 3 + 4 is 7.

Common mistakes

  • Reading MOD as the answer of a division. 17 MOD 5 is the leftover, 2. It is not 3, and it is not 3.4.
  • Using whole-number division in AP pseudocode. 17 / 5 is 3.4, not 3. The exam has no //.
  • Going strictly left to right. 2 + 3 * 4 is 14, not 20. Do *, / and MOD before + and -.
  • Forgetting that equal ranks go left to right. 20 - 6 - 2 is 12, not 16.
  • Getting a MOD b wrong when a is smaller than b. 3 MOD 5 is 3, because nothing can be taken out.
  • Dividing by zero. 5 / 0 and 5 MOD 0 have no answer. The program stops with an error.

On the exam

Expect to work out expressions that mix + - * / MOD and brackets, to say what DISPLAY shows, and to use MOD for "is it even?", "does it divide exactly?" and "wrap around" questions. Do it in small steps and write down each result. In MOD questions, always find the remainder, never the quotient.

Key words

Operator: a symbol that does a calculation, such as + or MOD.

Expression: code that is worked out to give one value.

Operand: a value an operator works on.

MOD: the operator that gives the remainder after a division.

Remainder: what is left after dividing as many whole times as possible.

Order of operations: the rules for which operators are done first.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.