# Java Array Tutorials
# Java Array Programs
➤ Count Even and Odd numbers in an Array
➤ Put Even and Odd Elements in 2 Separate Arrays
➤ Display Even and Odd numbers in an Array
➤ Sum of Even and Odd Numbers in Array in Java
➤ Remove odd numbers from an array java
➤ Count positive negative and zero from Array
➤ Print all negative elements in an array
➤ Separate positive and negative numbers in an array
➤ Find the sum of positive numbers in an array
➤ The cumulative sum of an array in Java
➤ Find numbers that are greater than the given number from an array
➤ Find average and numbers greater than average in array
Program description:- Write a Java program to put even and odd elements of an array in 2 separate arrays. And display both arrays.
Example:-
Array = {11, 12, 13, 14, 15}
Even numbers are:- 12, 14
Odd numbers are:- 11, 13, 15
Prerequisite:- Array in Java, find length of array in Java, different ways to print array in Java
In this program, we will take input value for the array from the end-user, but you can take it explicitly. See:- how to take array input in Java. We will use a method to perform the operation.
There are multiple ways to display an array but here we will use Arrays.toString() method given in java.util.Arrays class which is used to convert an array to String format. The java.util.Arrays class contains many methods related to array operations like sort an array using sort(), copy an array copyOf() or copyOfRange(), search an element in array using binary search, and e.t.c. Learn more about:- Arrays class and methods in Java.
Procedure to develop the Java method to put even and odd elements of Java array in 2 separate arrays,
a) Take an array
b) Count even and odd numbers in the given array. Assume they are countEven, and countOdd.
c) Create two arrays of size countEven and countOdd to store even and odd numbers separately.
d) Traverse through the original array.
e) Check each element of the array.
f) If the element is an even number then insert it in the even array (newly created array).
g) Else insert it in the odd array (newly created array).
h) Display both arrays.
Java Program to Put Even and Odd Elements of an Array in 2 Separate Arrays
import java.util.Arrays; import java.util.Scanner; public class ArrayTest { public static void main(String[] args) { // create Scanner class object to take input Scanner scan = new Scanner(System.in); // read size of the array System.out.print("Enter size of the array: "); int n = scan.nextInt(); // create an int array of size n int numbers[] = new int[n]; // take input for the array System.out.println("Enter array elements: "); for (int i = 0; i < n; ++i) { numbers[i] = scan.nextInt(); } // display odd-even displayOddEven(numbers); // close Scanner scan.close(); } // method to display even or odd in array public static void displayOddEven(int[] numbers) { // variables int countEven = 0; int countOdd = 0; int even[] = null; int odd[] = null; // count even numbers for (int i : numbers) { if (i % 2 == 0) ++countEven; } // count odd numbers countOdd = numbers.length - countEven; // create array to store odd and even numbers even = new int[countEven]; odd = new int[countOdd]; // check each element and insert // them in appropriate array int i = 0; int j = 0; for (int num : numbers) { if (num % 2 == 0) { // even even[i++] = num; } else { odd[j++] = num; } } // display even & odd arrays System.out.println("Even numbers: " + Arrays.toString(even)); System.out.println("Odd numbers: " + Arrays.toString(odd)); } }
Output:-
Enter size of the array: 5
Enter array elements:
11 12 13 14 15
Even numbers: [12, 14]
Odd numbers: [11, 13, 15]
Enter size of the array: 7
Enter array elements:
-15 -10 -5 0 5 10 15
Even numbers: [-10, 0, 10]
Odd numbers: [-15, -5, 5, 15]
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!