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:
  • true for even numbers
  • false for odd numbers

Example 1

Input
n (int) = 4
Returns (boolean) true

Example 2

Input
n (int) = 7
Returns (boolean) false

Example 3

Input
n (int) = 0
Returns (boolean) true

Example 4

Input
n (int) = -2
Returns (boolean) true

Example 5

Input
n (int) = -3
Returns (boolean) false

To check if a number is even:

  1. Use the modulus operator % to divide the number by 2.

  2. If the remainder is 0, the number is even.

  3. 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

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.