Remove Odd Numbers From Array in Java

Program description:- Write a Java program to remove odd numbers from an array. Display the resultant array.

Example1:-
Array = {11, 12, 13, 14, 15}
Array after removing odd numbers:- {12, 14}

Example2:-
Array = {-15, -10, -5, 0, 5, 10, 15}
Array after removing odd numbers:- {-10, 0, 10}

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.

Procedure to develop the Java method to remove odd numbers from an array,
a) Take an array
b) Count even numbers in the given array. Assume it is countEven.
c) Create one new array of size countEven.
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).
h) Return the new array.

Java Program to Remove Odd Numbers From a given 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();
    }

    // remove odd numbers
    int newArr[] = removeOdd(numbers);

    // display new array
    System.out.println("Array after removing odd numbers: " 
                      + Arrays.toString(newArr));

    // close Scanner
    scan.close();

  }

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

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

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

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

    // check each element and insert
    int i = 0;
    for (int num : numbers) {
      if (num % 2 == 0) { // even
        even[i++] = num;
      }
    }

    // return
    return even;
  }
}

Output:-

Enter the size of the array: 5
Enter array elements:
11 12 13 14 15
Array after removing odd numbers: [12, 14]

Enter the size of the array: 7
Enter array elements:
9 8 7 6 5 4 3
Array after removing odd numbers: [8, 6, 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 *