Put Even and Odd Elements of Java Array in 2 Separate Arrays

Program description:- Write a Java program to put even and odd elements of an array in 2 separate arrays. And display both arrays.

Example:-
Array = {11, 12, 13, 14, 15}
Even numbers are:- 12, 14
Odd numbers are:- 11, 13, 15

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 array using binary search, and e.t.c. Learn more about:- Arrays class and methods in Java.

Procedure to develop the Java method to put even and odd elements of Java array in 2 separate arrays,
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.

Java Program to Put Even and Odd Elements of an Array in 2 Separate 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 size of the array: 5
Enter array elements:
11 12 13 14 15
Even numbers: [12, 14]
Odd numbers: [11, 13, 15]

Enter 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 *