Print even & odd numbers from array list

Q. Write a program to print even and odd number from the array list.

Answer:

In this program we find the even and odd number from the given array. Then print it in two different array. We used if else condition to check the given number is even or not.

Even_Odd.java

class Even_Odd
{
        public static void main(String[] args)
        {
                int num;
                int arr[] = {12, 23, 34, 45, 56, 67, 78, 89, 91};
                System.out.println("Given array elements:");
                for (int i=0; i<arr.length; i++ )
                {
                        System.out.print(arr[i]+" ");
                }
                System.out.println("\n=======================");
                System.out.println("\nEven number of the array:");
                for (int i = 0;i<arr.length ;i++ )
                {
                        if (arr[i]%2 == 0)
                        {
                                System.out.print(arr[i]+" ");
                        }
                }
                System.out.println("\n==============");
                System.out.println("\nOdd number of the array:");
                for (int i = 0;i<arr.length ;i++ )
                {
                        if (arr[i] % 2 != 0)
                        {
                         System.out.print(arr[i]+" ");
                        }
                }
                System.out.println("\n==============");
        }
}


Output:

even