Beginner Level

Implement the method countEvenNumbers that returns how many even numbers are in an array.

Count how many even integers appear in an array. An even number is divisible by 2 with no remainder.

The task is designed to test careful handling of edge cases, not only the most common input.

  • Use only the first size elements.
  • Zero should be counted as even.
  • The array may contain positive and negative values.
Example 1
Input:
nums (int[]) = [1, 2, 3, 4, 5]
size (int) = 5
Return:
(int) 2
Example 2
Input:
nums (int[]) = [2, 4, 6]
size (int) = 3
Return:
(int) 3
Example 3
Input:
nums (int[]) = [1, 3, 5]
size (int) = 3
Return:
(int) 0
Example 4
Input:
nums (int[]) = [-2, -1, 0, 7]
size (int) = 4
Return:
(int) 2

Loop through the requested range and increase the answer whenever value % 2 == 0.

Run your code to see the result.