Is it even?

Easy
The course
Boolean Branching, Remainder
1

Understand

Before writing anything, get clear on what is being asked.

Think of it like this

Line the number up in pairs, like shoes. If everything pairs off with nothing left standing alone, the number is even. The remainder is just "how many are left standing".

One = assigns a value; two == compares two values. Mixing them up is the single most common beginner bug, and in some languages it silently does the wrong thing rather than erroring.

Write a function that takes a whole number and answers one question: is it even?

Give back `true` if it is even, and `false` if it is not. A number is even when dividing it by 2 leaves nothing over.

Example 1

Given n = 4

Answer true

4 divided by 2 is exactly 2, with nothing left over.

Example 2

Given n = 7

Answer false

7 divided by 2 leaves 1 over, so it is odd.

What you can rely on

  • The number may be negative
  • Zero counts as even
2

Write it

Write the function in the editor, then run it against the examples.

The editor on the right already has the function signature filled in for you. Write your answer inside it and press Run — that checks your code against the examples above only, so you can experiment freely.

Run your code to tick this step off.

Stuck? Take one hint at a time

Each one nudges you a little further. There is no reveal-the-answer button — working it out is the part that actually teaches you.

3

Own it

Submit against every test, then check you can explain why it works.

Check the remainder

O(1) time · O(1) space

Ask for the remainder after dividing by 2, then compare it to 0. The comparison itself produces true or false, so you can return it directly — no if statement needed.

Returning the comparison directly
return n % 2 == 0

Beginners often write: if the remainder is 0 return true, otherwise return false. That works and is not wrong — but the comparison already IS a true/false value, so returning it directly says the same thing with less noise.

Why does checking n % 2 == 0 work for negative numbers like -6?

Press Submit to run every test, including the hidden ones.