
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Separate Odd and Even Elements into Two Separate Arrays in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to separate odd and even elements into two separate arrays and print the result.
A number is said to be an even number if it is divisible by 2, else the number is an odd number.
Note − The array must be an integer array.
In this article, you will see how to separate even and odd numbers of an array into two different arrays by using Java programming language.
Let’s start.
To Show You Some Instances
Instance-1
Suppose the original array is {21, 53, 99, 9, 67, 66, 2, 91}
After separating the two arrays, the result will be −
Even numbers are: [66, 2] Odd numbers are: [21, 53, 99, 9, 67, 91]
Instance-2
Suppose the original array is {12, 23, 11, 64, 5, 87, 22, 67, 100}
After separating the two arrays, the result will be −
Even numbers are: [12, 64, 22, 100] Odd numbers are: [23, 11, 5, 87, 67]
Instance-3
Suppose the original array is {11, 22, 33, 44, 55, 66, 77, 88, 99}
After separating the two arrays, the result will be −
Even numbers are: [22, 44, 66, 88] Odd numbers are: [11, 33, 55, 77, 99]
Algorithm
Step 1 − Declare and initialize an integer array.
Step 2 − For even elements in an array, we check for “arr[i]%2==0”.
Step 3 − For even elements in an array, we check for “arr[i]%2==1”.
Step 4 − Print the elements of the array.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length
Below refers to the syntax of it −
array.length
where, ‘array’ refers to the array reference.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Array and for Loop
By Using Static Initialization of Array and while Loop
By Using User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array and for Loop
Example
In this approach, array elements will be initialized in the program. Then as per the algorithm separate odd and even elements into two separate arrays. Here, we have used for loop for traversing array.
public class Main{ //main method public static void main(String[] args){ //Declare and initialize the array elements int arr[] = { 21, 53, 99, 9, 67, 66, 2, 91 }; //get the length of the array int size = arr.length; //Logic for even array elements System.out.println("Even numbers are:"); for(int i=0; i<size; i++){ if(arr[i]%2==0){ //separates even numbers System.out.print(arr[i]+" "); } } //Logic for odd array elements System.out.println("\nOdd numbers are:"); for(int i=0; i<size; i++){ if(arr[i]%2==1){ //separates odd numbers System.out.print(arr[i]+" "); } } } }
Output
Even numbers are: 66 2 Odd numbers are: 21 53 99 9 67 91
Approach-2: By Using Static Initialization of Array and while Loop
Example
In this approach, array elements will be initialized in the program. Then as per the algorithm separate odd and even elements into two separate arrays. Here, we have used while loop for traversing array.
public class Main { //main method public static void main(String[] args){ //Declare and initialize the array elements int arr[] = { 21, 53, 99, 9, 67, 66, 2, 91 }; //get the length of the array int size = arr.length; //Logic for even array elements System.out.println("Even numbers are:"); int i=0; while(i<size){ if(arr[i]%2==0){ //separates even numbers System.out.print(arr[i]+" "); } i++; } //Logic for odd array elements System.out.println("\nOdd numbers are:"); i=0; while(i<size){ if(arr[i]%2==1){ //separates odd numbers System.out.print(arr[i]+" "); } i++; } } }
Output
Even numbers are: 66 2 Odd numbers are: 21 53 99 9 67 91
Approach-3: By Using User Defined Method
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm separate odd and even elements into two separate arrays.
public class Main{ //main method public static void main(String[] args){ //Declare and initialize the array elements int arr[] = {12, 23, 11, 64, 5, 87, 22, 67, 100}; // calling the method printArray(arr); } //user defined method to separate odd and even elements public static void printArray(int []arr){ //get the length of the array int size = arr.length; //Logic for even array element System.out.println("Even numbers are:"); for(int i=0; i<size; i++){ if(arr[i]%2==0){ //separates even numbers System.out.print(arr[i]+" "); } } //Logic for odd array elements System.out.println("\nOdd numbers are:"); for(int i=0; i<size; i++){ if(arr[i]%2==1){ //separates odd numbers System.out.print(arr[i]+" "); } } } }
Output
Even numbers are: 12 64 22 100 Odd numbers are: 23 11 5 87 67
In this article, we explored how to separate odd and even elements of an array into two separate arrays by using Java programming language.
- Related Articles
- Separate odd and even in JavaScript
- C program to store even, odd and prime numbers into separate files
- Java program to split the Even and Odd elements into two different lists
- Missing even and odd elements from the given arrays in C++
- Python program to split the even and odd elements into two different lists.
- Python Program to Put Even and Odd elements in a List into Two Different Lists
- How to separate even and odd numbers in an array by using for loop in C language?
- C# program to split the Even and Odd integers into different arrays
- Pushing positives and negatives to separate arrays in JavaScript
- How to unpack array elements into separate variables using JavaScript?
- Update two separate arrays in a document with one update call in MongoDB?
- Check Average of Odd Elements or Even Elements are Greater in Java
- Swap Even Index Elements And Odd Index Elements in Python
- Split keys and values into separate objects - JavaScript
- Sorting odd and even elements separately JavaScript
