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 odd or even program user first enters the size of the array. Then using “for” loop array elements are stored in an array “arrNum”.

Also read – multidimensional array in java

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

Let’s even odd program in java using for loop,

// java odd or even program
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 in java
        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 in java
        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