Java Program to Count Even and Odd Numbers in an Array

Program description:- Write a Java program to count the even and odd numbers in the given array. Use a method to perform this operation. Take the array, and count even-odd numbers, and display it.

Example1:-
Array = {11, 12, 13, 14, 15}
Count of the even number = 2
Count of the odd number = 3

Example2:-
Array = {-15, -10, -5, 0, 5, 10, 15}
Count of the Even numbers = 3
Count of the Odd numbers = 4

Prerequisite:- Array in Java, find length of array in Java, different ways to print array in Java

In this program, we will take input value for the array from the end-user, but you can take it explicitly. See:- how to take array input in Java. We will use a method to perform the operation. Let us see the program. 

Procedure to develop a Java method to count the odd and even numbers in the given array,
a) Take an array
b) Take two variables to count the odd and even numbers and initialize it with the value 0. Assume they are oddCount, and evenCount
c) Traverse to the array
d) Check each element of the array whether it is even or not.
e) If it is an even number then increase the value of evenCount variable by 1.
f) Else it is an odd number and increases the value of oddCount variable by 1.
g) Display the resultant value of oddCount and evenCount variables.

Count Even and Odd Numbers in an Array in Java

import java.util.Scanner;

public class ArrayTest {

  public static void main(String[] args) {
    // create Scanner class object to take input
    Scanner scan = new Scanner(System.in);

    // read size of the array
    System.out.print("Enter size of the array: ");
    int n = scan.nextInt();

    // create an int array of size n
    int numbers[] = new int[n];

    // take input for the array
    System.out.println("Enter array elements: ");
    for (int i = 0; i < n; ++i) {
      numbers[i] = scan.nextInt();
    }

    // count odd-even
    countOddEven(numbers);

    // close Scanner
    scan.close();

  }

  // method to count odd and even numbers
  // in a given array and display it
  public static void countOddEven(int[] arr) {

    // variables
    int oddCount = 0;
    int evenCount = 0;

    // traverse the array
    for (int i = 0; i < arr.length; ++i) {
      if (arr[i] % 2 == 0) // even
        ++evenCount;
      else                 // odd
        ++oddCount;
    }

    // display result
    System.out.println("Count of the Even numbers = " 
                       + evenCount);
    System.out.println("Count of the Odd numbers = " 
                       + oddCount);

  }

}

Output:-

Enter the size of the array: 5
Enter array elements:
11 12 13 14 15
Count of the Even numbers = 2
Count of the Odd numbers = 3

Enter the size of the array: 7
Enter array elements:
-15 -10 -5 0 5 10 15
Count of the Even numbers = 3
Count of the Odd numbers = 4

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *