Let’s learn count number of even and odd elements in an array in java.

## Count number of even and odd elements in an array in java

To count the even and odd numbers in an array first user is allowed to enter size and elements of one dimensional array using nextInt() method of Scanner class.

To find given number is even or odd, number is divided by 2. If remainder is 0 then it is even number else odd number.

Finally number of even and odd numbers in the given array is counted and printed on console. Let’s see java program.

import java.util.Scanner; public class CountEvenOddNumbersInArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int arrSize, a, countEven = 0, countOdd = 0; System.out.println("Please enter number of elements of an array: "); arrSize = sc.nextInt(); int[] arrNumbers = new int[arrSize]; System.out.println("Please enter " + arrSize + " elements of an array: "); for(a = 0; a < arrSize; a++) { arrNumbers[a] = sc.nextInt(); } for(a = 0; a < arrNumbers.length; a++) { if(arrNumbers[a] % 2 == 0) { countEven++; } else { countOdd++; } } System.out.println("Total number of even numbers in an array java: " + countEven); System.out.println("Total number of odd numbers in an array java: " + countOdd); sc.close(); } }

**Output:**

Please enter number of elements of an array: 5

Please enter 5 elements of an array: 1

2

3

4

5

Total number of even numbers in an array java: 2

Total number of odd numbers in an array java: 3

Also read – inheritance in java