Lesson 4.3 · Write it
Is even
A functionPython or Java7 tests, 4 of them hidden
On a phone? Put the lines in order instead
Write a function that returns whether a whole number is even.
nis a whole number that can be negative, zero, or positive.- Return
True(Python) ortrue(Java) ifnis divisible by 2 with no remainder,Falseorfalseotherwise. - Zero is even.
Only the value your function returns is checked.
def is_even(n: int) -> bool
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- is_even(4) should return True: a positive even number (rule 2)
- is_even(7) should return False: an odd number (rule 2)
- is_even(0) should return True: zero is even (rule 3)
public static boolean isEven(int n)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- isEven(4) should return true: a positive even number (rule 2)
- isEven(7) should return false: an odd number (rule 2)
- isEven(0) should return true: zero is even (rule 3)