Java program to print odd and even numbers in an array

Let’s learn java program to print odd and even numbers in an array.

Java program to print odd and even numbers in an array

In java program to print odd and even numbers in an array first user enters number of elements in an array using nextInt() method of Scanner class.

Then using for loop array elements are stored in an integer array “arrNum”.

Then using for loop and if condition odd or even is determined from given integer array.

Let’s learn java program to print odd and even numbers 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();
        }
        // print odd numbers
        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("");
        // print even numbers
        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 numbers in an array

Also read – variables in java