Tutorial 3
➕️
Arithmetic Operators
2210 O-Level
Within our programs, we will often have to perform calculations - this is where we need arithmetic (mathematical) operators. Most everyone should already know, a few perhaps not
1
All Operators
Let's see the 7 arithmetic operators in IG - note that DIV and MOD are functions for IG
- +: addition
- -: subtraction
- *: multiplication
- /: division
- ^: exponentiation (power)
- DIV: returns the quotient of one number divided by another - i.e. how many times the number can be wholly divided by the other
- MOD: returns the remainder after the integer division (DIV)
People often get confused about DIV and MOD - imagine sharing 14 slices of pizza among 5 people:
- DIV(14, 5) = 2 - i.e. how many whole slices each person gets
- MOD(14, 5) = 4 - i.e. how many slices are left over, after everyone has been given their 2 whole slices
DECLARE a, b : INTEGER
a <-- 6
b <-- 4
OUTPUT a + b
OUTPUT a - b
OUTPUT a * b
OUTPUT a / b
OUTPUT DIV(a, b)
OUTPUT MOD(a, b)
OUTPUT a ^ b
2
Favourite Numbers
Create a program that asks the user to enter their 3 favourite numbers, then will output the result of each arithmetic operation on all 3 of them - i.e. adding all 3, subtracting all 3 etc
DECLARE a, b, c : INTEGER
OUTPUT "Enter first number"
INPUT a
OUTPUT "Enter second number"
INPUT b
OUTPUT "Enter third number"
INPUT c
OUTPUT a + b + c
OUTPUT a - b - c
OUTPUT a * b * c
OUTPUT a / b / c
OUTPUT DIV(DIV(a, b), c)
OUTPUT MOD(MOD(a, b), c)
OUTPUT a ^ b ^ c