Java Program to Display Even and Odd Numbers in Array

Program description:- Write a Java program to display even and odd numbers from a given array of numbers. Take an array, check each element of the array whether is it even or odd and display it.

Example:-
Array = {11, 12, 13, 14, 15}
11: odd, 12: even, 13: odd, 14: even, 15: odd

The same can be displayed in another form as,
Array = {11, 12, 13, 14, 15}
Even numbers are:- 12, 14
Odd numbers are:- 11, 13, 15

We will develop two programs. The first program will display each element whether it is even or odd. The second program will display all even elements at once, and similarly all odds elements at once.

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. 

There are multiple ways to display an array but here we will use Arrays.toString() method given in java.util.Arrays class which is used to convert an array to String format. The java.util.Arrays class contains many methods related to array operations like sort an array using sort(), copy an array copyOf() or copyOfRange(), search an element in an array using binary search, and e.t.c. Learn more about:- Arrays class and methods in Java.

Java Program to Display Even or Odd in Array

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();
    }

    // display odd-even
    displayOddEven(numbers);

    // close Scanner
    scan.close();

  }

  // method to display even or odd in array
  public static void displayOddEven(int[] numbers) {
    // traverse through the array
    for (int i : numbers) {
      if(i%2 == 0)       // even
        System.out.println(i+": Even");
      else               // odd
        System.out.println(i+": Odd");
    }   
  }
}

Output:-

Enter the size of the array: 5
Enter array elements:
11 12 13 14 15
11: Odd
12: Even
13: Odd
14: Even
15: Odd

Enter the size of the array: 5
Enter array elements:
-15 -9 -4 0 50
-15: Odd
-9: Odd
-4: Even
0: Even
50: Even

Display All Even and Odd at once

Now, let us develop another program to display even and odd at once. The procedure,
a) Take an array
b) Count even and odd numbers in the given array. Assume they are countEven, and countOdd.
c) Create two arrays of size countEven and countOdd to store even and odd numbers separately.
d) Traverse through the original array.
e) Check each element of the array.
f) If the element is an even number then insert it in the even array (newly created array).
g) Else insert it in the odd array (newly created array).
h) Display both arrays.

import java.util.Arrays;
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();
    }

    // display odd-even
    displayOddEven(numbers);

    // close Scanner
    scan.close();

  }

  // method to display even or odd in array
  public static void displayOddEven(int[] numbers) {

    // variables
    int countEven = 0;
    int countOdd = 0;
    int even[] = null;
    int odd[] = null;

    // count even numbers
    for (int i : numbers) {
      if (i % 2 == 0)
        ++countEven;
    }
    // count odd numbers
    countOdd = numbers.length - countEven;

    // create array to store odd and even numbers
    even = new int[countEven];
    odd = new int[countOdd];

    // check each element and insert 
    // them in appropriate array
    int i = 0;
    int j = 0;
    for (int num : numbers) {
      if (num % 2 == 0) { // even
        even[i++] = num;
      } else {
        odd[j++] = num;
      }
    }
    
    // display even & odd arrays
    System.out.println("Even numbers: " 
                      + Arrays.toString(even));
    System.out.println("Odd numbers: " 
                      + Arrays.toString(odd));
  }
}

Output:-

Enter the size of the array: 5
Enter array elements:
11 12 13 14 15
Even numbers: [12, 14]
Odd numbers: [11, 13, 15]

Enter the size of the array: 7
Enter array elements:
-15 -10 -5 0 5 10 15
Even numbers: [-10, 0, 10]
Odd numbers: [-15, -5, 5, 15]

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 *