Program to count number of even and odd elements in an array

Here we will discuss to count number of even and odd elements in an array.

  • Create the scanner class object to pass user input.
  • Take two variables, evenNumber=0, oddNumber=0.
  • Traverse the array data from for loop.
  • Increment evenNumber value if(arr[i]%2==0)
  • Otherwise increment the oddNumber value of countOdd by 1.
				
					import java.util.Scanner;

public class EvenOddArrayElement {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int i, evenNumber = 0, oddNumber = 0;
		int a[] = new int[5];
		System.out.println("enter the number of elements :- ");
		int n = sc.nextInt();

		System.out.println("enter the elements :-");
		for (i = 0; i < n; i++) {
			a[i] = sc.nextInt();

			if (a[i] % 2 == 0) {
				evenNumber++;
			} else {
				oddNumber++;
			}

		}
		System.out.println("Total numbers of even are :- " + evenNumber);
		System.out.println("Total numbers of odd are :- " + oddNumber);
	}

}
				
			

Output :-
enter the number of elements :-
3
enter the elements :-
25
45
80
Total numbers of even are :- 1
Total numbers of odd are :- 2