count number of even and odd elements in an array in java

Solutions on MaxInterview for count number of even and odd elements in an array in java by the best coders in the world

showing results for - "count number of even and odd elements in an array in java"
Modesty
16 Aug 2017
1// count number of even and odd elements in an array in java
2import java.util.Scanner;
3public class CountEvenOddNumbersInArray
4{
5   public static void main(String[] args)
6   {
7      Scanner sc = new Scanner(System.in);
8      int arrSize, a, countEven = 0, countOdd = 0;
9      System.out.println("Please enter number of elements of an array: ");
10      arrSize = sc.nextInt();
11      int[] arrNumbers = new int[arrSize];
12      System.out.println("Please enter " + arrSize + " elements of an array: ");
13      for(a = 0; a < arrSize; a++)
14      {
15         arrNumbers[a] = sc.nextInt();
16      }
17      for(a = 0; a < arrNumbers.length; a++)
18      {
19         if(arrNumbers[a] % 2 == 0)
20         {
21            countEven++;
22         }
23         else
24         {
25            countOdd++;
26         }
27      }
28      System.out.println("Total number of even numbers in an array java: " + countEven);
29      System.out.println("Total number of odd numbers in an array java: " + countOdd);
30      sc.close();
31   }
32}