Tutorial 3

➕️

Arithmetic Operators

9618 A-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 A-Level

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • ** exponentiation (power) - exams often use ^ too, but the website uses **, since the ^ operator is used for pointers
  • 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:

  • 14 DIV 5 = 2 - i.e. how many whole slices each person gets
  • 14 MOD 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 a DIV b OUTPUT a MOD 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 (a DIV b) DIV c OUTPUT (a MOD b) MOD c OUTPUT a ** b ** c