Even or odd? (mod and if-else)
Section 12: Operators and timeLesson 12.1 · ⏱ 8 min
mod gives the remainder of a division: 7 mod 2 = 1, and 8 mod 2 = 0. So a number is even if its remainder when divided by 2 is zero. if … else runs one part if the condition is true, and the other part if it isn't. join sticks two pieces of text together.


📌 Another example: big or small?
Robo picks a number from 1 to 100: if it is more than 50 it is big, else it is small.


✍️ Practise
Run the example 5 times and check every number yourself. Then make Robo say whether the number can be divided by 3.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
Count from 1 to 10, but Robo says only the even numbers (i mod 2 = 0).
👀 Show the solution🙈 Hide the solution


Medium
From 1 to 30: Robo says only the multiples of 5 (5, 10, 15…).
👀 Show the solution🙈 Hide the solution


Hard
The “Fizz Buzz” game from 1 to 15: a multiple of 3 says “Fizz”, a multiple of 5 says “Buzz”, a multiple of both (15) says “Fizz Buzz”, and anything else says the number.
👀 Show the solution🙈 Hide the solution



