Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program To Put Even and Odd Elements of an Array in Two Separate Arrays

In this tutorial, we will learn how to put the even and odd elements in two separate arrays. The easiest way to do this is by first checking the index of both the arrays by counting the number of even and odd terms in the array respectively and then pasting these elements in the newly formed arrays. 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: 7 6 9 2 4 1 3 6 9 8

Output:

Even Array: 6 2 4 6 8

Odd Array: 7 9 1 3 9

Program 1: Put the Even and Odd Elements in Two Separate Arrays

In this approach, we will directly put the even and odd elements of the array in two separate arrays in the main method itself.

Algorithm

1.Start

2. Declare an array.

3. Initialize the array.

4. Declare two variables to store the sizes of even and odd array, and initialize them to zero.

5. Count the total number of even and odd elements and assign it to the array size.

6. Declare the two new arrays to store the even and odd elements.

7. Using a for loop iterate through all the elements

8. If the element is even then store it in the first array and if the element is odd then store it in the second array.

9. Display both the arrays.

10. Stop.

Below is the code for the same.

Program 2: Example to put the even and odd elements in two separate arrays

The below program demonstrates how to directly put the even and odd elements in two separate arrays. Firstly, an array is declared and then initialized. Then the number of even and odd elements are counted. Two new arrays with these sizes are declared and the even and odd elements are copied to these arrays. In the end, these two separate arrays are printed.

/*Java Program to put the even and odd elements in two separate array*/
import java.util.Scanner;

public class findElement
{
     public static void main(String []args)
     {
         Scanner sc=new Scanner(System.in);
         int n;     //Declare array size
         System.out.println("Enter the size of the array");
         n=sc.nextInt();   //Initialize array size
         
         int arr[]=new int[n];   //Declare array 
        System.out.println("Enter the array");  
        for(int i=0;i<n;i++)     //Initialize array
        {
            arr[i]=sc.nextInt();
        }
        
        int m=0,n1=0;    //Declare the size of the array for even and odd elements
        for(int i=0;i<n;i++)   
        {
            if(arr[i]%2==0)
             m++;    //Increment even array size
             else 
             n1++;   //Increment odd array size
        }
        int even[]=new int[m];    //Declare an even array
        int odd[]=new int[n1];   //Declare an odd array
        
        int k=0,t=0;
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2==0)
            {
              even[k]=arr[i];   //Initialize elements of even array
              k++;
            }
            else
            {
                odd[t]=arr[i];  //Initialize elements of odd array
                t++;
            }
        }
        
        System.out.println("The array with even elements...");
        for(int i=0;i<m;i++)
        {
            System.out.print(even[i]+" ");   //Print Even Array
        }
        System.out.println("");
        System.out.println("The array with odd elements...");
        for(int i=0;i<n1;i++)
        {
            System.out.print(odd[i]+" ");    //Print Odd Array
        }
        
    }
}


Enter the size of the array 10
Enter the array 87 6 7 5 4 3 4 32 12 1
The array with even elements...
6 4 4 32 12
The array with odd elements...
87 7 5 3 1

Program 2: Put the Even and Odd Elements in Two Separate Arrays

In this approach, we will use a method to put the even and odd elements of the array in two separate arrays and another method to print these elements.

Algorithm

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Call a method that will put the even and odd elements of the array in two separate arrays.
  5. Declare two variables in that to store the sizes of even and odd array, and initialize them to zero.
  6. Count the total number of even and odd elements and assign it to the array size.
  7. Declare the two new arrays to store the even and odd elements.
  8. Using a for loop iterate through all the elements
  9. If the element is even then store it in the first array and if the element is odd then store it in the second array.
  10. Call another method to print these two arrays.
  11. Stop

Below is the code for the same.

The below program demonstrates how to put the even and odd elements in two separate arrays using different methods. Firstly, an array is declared and then initialized. Then a method is called and the number of even and odd elements are counted. Two new arrays with these sizes are declared and the even and odd elements are copied to these arrays. Another method is then called, to print these arrays.

/*Java Program to put the even and odd elements in two separate arrays*/

import java.util.Scanner;

public class findElement
{
    //Method to print the even and odd array
    static void printArray(int even[], int m, int odd[],int n1)
    {
        System.out.println("The array with even elements...");
        for(int i=0;i<m;i++)
        {
            System.out.print(even[i]+" ");   //Print Even Array
        }
        System.out.println("");
        System.out.println("The array with odd elements...");
        for(int i=0;i<n1;i++)
        {
            System.out.print(odd[i]+" ");    //Print Odd Array
        }
        
    }
    
    //Method to put even and odd elements in different arrays
    static void separate(int arr[],int n)    
    {
         int m=0,n1=0;    //Declare the size of the array for even and odd elements
         for(int i=0;i<n;i++)   
         {
            if(arr[i]%2==0)
             m++;    //Increment even array size
             else 
             n1++;   //Increment odd array size
         }
        int even[]=new int[m];    //Declare an even array
        int odd[]=new int[n1];   //Declare an odd array
        
        int k=0,t=0;
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2==0)
            {
              even[k]=arr[i];   //Initialize elements of even array
              k++;
            }
            else
            {
                odd[t]=arr[i];  //Initialize elements of odd array
                t++;
            }
        }
        printArray(even,m,odd,n1);
    }

     //Driver Method
     public static void main(String []args)
     {
         Scanner sc=new Scanner(System.in);
         int n;     //Declare array size
         System.out.println("Enter the size of the array");
         n=sc.nextInt();   //Initialize array size
         
         int arr[]=new int[n];   //Declare array 
        System.out.println("Enter the array");  
        for(int i=0;i<n;i++)     //Initialize array
        {
            arr[i]=sc.nextInt();
        }
        
        separate(arr,n);
        
    }
}


Enter the size of the array 10
Enter the array 78 65 43 45 3 21 78 88 24 12
The array with even elements...
78 78 88 24 12
The array with odd elements...
65 43 45 3 21