Signup/Sign In
LAST UPDATED: MARCH 5, 2021

Java Program To Print the Odd and Even Numbers in an Array

In this tutorial, we will learn how to print the even and odd numbers of an array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: Enter the array elements: 5 4 3 2 6 7 8 9 4 2 1

Output: Even Elements: 4 2 6 8 4 2

Odd Elements: 5 3 7 9 1

Program 1: Find the Even and Odd Elements of an Array

In this approach, we will see how to find the even and odd elements of an array in the main method.

Algorithm

  1. Start
  2. Declare an array size.
  3. Ask the user to initialize the array size.
  4. Declare an array.
  5. Ask the user to initialize the array elements.
  6. Print the even elements by checking the conditions using for loop.
  7. Print the odd elements by checking the conditions using for loop.
  8. Stop

Below is the code for the same.

The below example demonstrates how to find the even and odd elements of an array.

// Java Program to Print the even and odd Element of the Array 
  
import java.io.*; 
import java.util.Scanner; 
  
public class Main 
{ 
    public static void main(String[] args) 
    { 
        //Take input from the user
        Scanner sc=new Scanner(System.in);
        
        int n;    //Declare size of an array
        System.out.println("Enter the size of the array: ");
        n=sc.nextInt();    //Intialize the array size
        
        int arr[]=new int[n];   //Declare an array
        System.out.println("Enter the array elements: ");
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();    //Initialize the array elements
        }
        //Print the even elements
        System.out.println("The Even Elements are...");
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2==0)   //Check whether even or not
            {
                System.out.print(arr[i]+" ");
            }
        }
        System.out.println(" ");
        
        //Print the odd elements
        System.out.println("The Odd Elements are...");
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2!=0)   //check whether odd or not
            {
                System.out.print(arr[i]+" ");
            }
        }
    }
}


Enter the size of the array: 10
Enter the array elements: 1 3 2 4 4 3 1 6 8 9
The Even Elements are...
2 4 4 6 8
The Odd Elements are...
1 3 3 1 9

Program 2: Find the Even and Odd Elements of an Array

In this approach, we will see how to find the even and odd elements of an array by using separate methods for each of them.

Algorithm

  1. Start
  2. Declare an array size.
  3. Ask the user to initialize the array size.
  4. Declare an array.
  5. Ask the user to initialize the array elements.
  6. Call a method to check for the even elements.
  7. Print the even elements.
  8. Call another method to check for the odd elements.
  9. Print the odd elements.
  10. Stop

Below is the code for the same.

The below example demonstrates how to find the even and odd elements of an array by using separate methods.

// Java Program to Print the even and odd Element of the Array 
  
import java.io.*; 
import java.util.Scanner; 
  
public class Main 
{ 
    //Driver Method
    public static void main(String[] args) 
    { 
        //Take input from the user
        Scanner sc=new Scanner(System.in);
        
        int n;    //Declare size of an array
        System.out.println("Enter the size of the array: ");
        n=sc.nextInt();    //Intialize the array size
        
        int arr[]=new int[n];   //Declare an array
        System.out.println("Enter the array elements: ");
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();    //Initialize the array elements
        }
        System.out.println("The Even Elements are...");
        printEven(arr,n);    //Method to print even elements
        System.out.println(" ");
        System.out.println("The Odd Elements are...");
        printOdd(arr,n);    //Method to print odd elements
           
    } 
    
    //Method to print the even elements
    static void printEven(int arr[], int n)
    {
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2==0)
            {
                System.out.print(arr[i]+" ");
            }
        }
    }
    
    //Method to print the odd elements 
    static void printOdd(int arr[], int n)
    {
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2!=0)
            {
                System.out.print(arr[i]+" ");
            }
        }
    }
}


Enter the size of the array: 10
Enter the array elements: 6 9 8 7 3 4 5 2 1 2
The Even Elements are...
6 8 4 2 2
The Odd Elements are...
9 7 3 5 1



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.