MOD function

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

MOD function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does MOD(10, 3) return?
A 1
B 3
2. Which function returns the remainder after division?
A MOD(a, b)
B DIV(a, b)
3. Which condition tests whether the integer n is even?
A MOD(n, 2) = 0
B MOD(n, 2) = 1
4. What does MOD(12, 4) return?
A 3
B 0
5. What does MOD(7, 2) return?
A 3
B 1

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1MOD(10, 3) AChecks whether n is an even number
2IF MOD(n, 2) = 0 THEN BMOD returns the remainder, not the whole-number quotient. MOD(10, 3) is 1. Use DIV(10, 3) to get 3.
3Find the remainder when 17 is divided by 5 CGives 1, because 10 divided by 3 is 3 remainder 1
4Test whether a whole number Num is odd DMOD(17, 5)
5Answer ← MOD(10, 3) OUTPUT Answer // expects 3 EA remainder of 1 means n is odd, not even. To test for even, the condition should be MOD(n, 2) = 0.
6IF MOD(n, 2) = 1 THEN OUTPUT "Even" ENDIF FIF MOD(Num, 2) = 1 THEN

Definitions

MOD(a, b)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
Remainder ← ___(10, 3)
2.
IF MOD(Number, _) = 0 THEN OUTPUT "Even" ENDIF
3.
IF MOD(Number, 2) __ 0 THEN OUTPUT "Odd" ENDIF
4.
DECLARE Remainder : _______ Remainder ← MOD(10, 3)
5.
IF MOD(Number, 2) = _ THEN OUTPUT "Even" ENDIF

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Number : INTEGER
B INPUT Number
C IF MOD(Number, 2) = 0 THEN
D OUTPUT "Odd"
E ENDIF
Question 2
A INPUT Number
B IF MOD(Number, 2) = 1 THEN
C OUTPUT "Even"
D ENDIF
Question 3
A DECLARE Remainder : INTEGER
B Remainder ← MOD(10 3)
C OUTPUT Remainder
Question 4
A Total ← 0
B FOR Number ← 1 TO 10
C IF MOD(Number, 2) == 0 THEN
D Total ← Total + Number
E ENDIF
F NEXT Number
Question 5
A INPUT Hours
B Hours ← Hours + 5
C Hours ← MOD(Hours, 24)
D OUTPUT "Hours"

Answer Key

MOD function
Section A: Quick Questions
Q1
A — 1
Q2
A — MOD(a, b)
Q3
A — MOD(n, 2) = 0
Q4
B — 0
Q5
B — 1
Section B: Match the Pairs
Q1
MOD(10, 3) → Gives 1, because 10 divided by 3 is 3 remainder 1
Q2
IF MOD(n, 2) = 0 THEN → Checks whether n is an even number
Q3
Find the remainder when 17 is divided by 5 → MOD(17, 5)
Q4
Test whether a whole number Num is odd → IF MOD(Num, 2) = 1 THEN
Q5
Answer ← MOD(10, 3) OUTPUT Answer // expects 3 → MOD returns the remainder, not the whole-number quotient. MOD(10, 3) is 1. Use DIV(10, 3) to get 3.
Q6
IF MOD(n, 2) = 1 THEN OUTPUT "Even" ENDIF → A remainder of 1 means n is odd, not even. To test for even, the condition should be MOD(n, 2) = 0.
Section C: Definitions
Q1
MOD(a, b) — Returns the remainder when a is divided by b. For example MOD(10, 3) gives 1. MOD(n, 2) = 0 means n is even.
Section D: Fill in the Blanks
Q1
Remainder ← MOD(10, 3)
Q2
IF MOD(Number, 2) = 0 THEN OUTPUT "Even" ENDIF
Q3
IF MOD(Number, 2) <> 0 THEN OUTPUT "Odd" ENDIF
Q4
DECLARE Remainder : INTEGER Remainder ← MOD(10, 3)
Q5
IF MOD(Number, 2) = 0 THEN OUTPUT "Even" ENDIF
Section E: Spot the Error
Q1
D — MOD(Number, 2) = 0 means there is no remainder when dividing by 2, so the number is even. The output should be "Even".
Q2
B — A remainder of 1 when dividing by 2 means the number is odd. To test for even numbers the condition must be MOD(Number, 2) = 0.
Q3
B — MOD takes two values separated by a comma. It should be MOD(10, 3), which gives 1.
Q4
C — Cambridge pseudocode uses a single = to compare values. The line should be IF MOD(Number, 2) = 0 THEN.
Q5
D — Quotation marks make OUTPUT print the word Hours instead of the value stored in the variable. It should be OUTPUT Hours.