Java program to print odd and even numbers in an array

We are going to learn java program to print odd and even numbers in an array.

Java program to print odd and even numbers in an array

In this java program user first enters the size of the array. Then using “for” loop array elements are stored in “arrNum”.

Also read – Multidimensional array in java

To distinguish between odd and even number modulus operator is used. Using “for” loop and “if” condition odd number and even number is determined.

Let’s see java program to find odd or even in an array,

import java.util.Scanner;

public class EvenOddArray
{
    public static void main(String[] args)
    {
        int numbers;
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter elements in array : ");
        numbers = sc.nextInt();
        int arrNum[] = new int[numbers];
        System.out.println("Enter " + numbers + " elements : ");
        for(int a = 0; a < numbers; a++) 
        {
            arrNum[a] = sc.nextInt();
        }
        System.out.print("Odd numbers : ");
        for(int a = 0 ; a < numbers ; a++)
        {
            if(arrNum[a] % 2 != 0)
            {
                System.out.print(arrNum[a] + " ");
            }
        }
        System.out.println("");
        System.out.print("Even numbers : ");
        for(int a = 0 ; a < numbers ; a++)
        {
            if(arrNum[a] % 2 == 0)
            {
                System.out.print(arrNum[a] + " ");
            }
       }
        sc.close();
    }
}

Output:

java program to print odd and even number in an array