Beginner Level
Implement the method isEven that takes an integer as input and returns true if the number is even, and false if it is odd.
You need to write a function that checks whether a given integer is even or not. A number is called even if it is divisible by 2 (e.g., 0, 2, 4, 6...). If it's not divisible by 2, it is odd (e.g., 1, 3, 5...). The method should return:
truefor even numbersfalsefor odd numbers
Example 1
Input
n (int) =
4Returns (boolean)
trueExample 2
Input
n (int) =
7Returns (boolean)
falseExample 3
Input
n (int) =
0Returns (boolean)
trueExample 4
Input
n (int) =
-2Returns (boolean)
trueExample 5
Input
n (int) =
-3Returns (boolean)
falseTo check if a number is even:
-
Use the modulus operator
%to divide the number by 2. -
If the remainder is
0, the number is even. -
Otherwise, it’s odd.
Pseudocode:
Function isEven(n):
If n mod 2 equals 0:
Return true
Else:
Return false
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run