Module 3: Arithmetic Operations
Standard Arithmetic
ERL supports the standard math operations you know:
+: Addition-: Subtraction*: Multiplication/: Division^: Exponentiation (e.g.,2 ^ 3is 8)
Exercise: Basic Math
Calculate 5 squared using the exponentiation operator and print the result.
ex1.erl
DIV and MOD
These are two very important operations in GCSE Computer Science.
DIV (Integer Division)
DIV tells you how many times a number goes into another cleanly, ignoring any remainder.
print(10 DIV 3) // Outputs 3 (because 3x3=9)
print(10 DIV 5) // Outputs 2
MOD (Modulo)
MOD finds the remainder after division. Extremely useful for checking if a number is even/odd (if x MOD 2 == 0).
print(10 MOD 3) // Outputs 1 (because 10 / 3 is 3 remainder 1)
print(10 MOD 5) // Outputs 0 (no remainder)
Exercise: Even or Odd Checker
Write an algorithm that asks the user for a number, casts it to an integer, and calculates the remainder when divided by 2 using MOD. Print out that remainder.
ex2.erl