Java Program to find even and odd elements in array


In this program, You will learn how to find even and odd elements in array in Java.


List is: 1 2 3 4 5

Even list: 2 4

Odd list: 1 3 5


Example: How to find even and odd elements in array in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        int i, s = 0;
        int arr[] = new int[5];
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter 5 Numbers :");

        for (i = 0; i < 5; i++) {
            arr[i] = sc.nextInt();
        }

        /* Find Even List */
        System.out.print("\nEven List is :");
        for (i = 0; i < 5; i++) {
            if (arr[i] % 2 == 0) {
                System.out.print(" " + arr[i]);
            }
        }

        /* Find Odd List */
        System.out.print("\nOdd List is :");
        for (i = 0; i < 5; i++) {
            if (arr[i] % 2 != 0) {
                System.out.print(" " + arr[i]);
            }
        }

    }
}

Output:

Enter 5 Numbers :1 2 3 4 5
Even List is : 2 4
Odd List is : 1 3 5