Introduction
Separating even and odd numbers in an array involves creating two separate groups within the array, one for even numbers and the other for odd numbers. This task can be useful in scenarios where different operations are needed on even and odd numbers. This guide will walk you through writing a Java program that separates even and odd numbers in an array.
Problem Statement
Create a Java program that:
- Prompts the user to enter the size of an array and its elements.
- Separates the even and odd numbers within the array.
- Displays the even numbers followed by the odd numbers in the array.
Example:
- Input:
[1, 2, 3, 4, 5, 6]
- Output:
"Array after separating even and odd numbers: [2, 4, 6, 1, 3, 5]"
Solution Steps
- Read the Array Size and Elements: Use the
Scanner
class to take the size and elements of the array as input from the user. - Separate Even and Odd Numbers: Use a loop to traverse the array and rearrange the elements so that even numbers are moved to the front and odd numbers to the back.
- Display the Result: Print the array with even numbers followed by odd numbers.
Java Program
// Java Program to Separate Even and Odd Numbers in an Array // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class SeparateEvenOdd { public static void main(String[] args) { // Step 1: Read the size and elements of the array from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int size = scanner.nextInt(); int[] array = new int[size]; System.out.println("Enter the elements of the array:"); for (int i = 0; i < size; i++) { array[i] = scanner.nextInt(); } // Step 2: Separate even and odd numbers separateEvenOdd(array); // Step 3: Display the array with even numbers followed by odd numbers System.out.println("Array after separating even and odd numbers:"); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } // Method to separate even and odd numbers in an array public static void separateEvenOdd(int[] array) { int left = 0; int right = array.length - 1; while (left < right) { // Increment left index while we see an even number at the left while (array[left] % 2 == 0 && left < right) { left++; } // Decrement right index while we see an odd number at the right while (array[right] % 2 == 1 && left < right) { right--; } // If left is less than right, then swap array[left] and array[right] if (left < right) { int temp = array[left]; array[left] = array[right]; array[right] = temp; left++; right--; } } } }
Explanation
Step 1: Read the Array Size and Elements
- The
Scanner
class is used to read the size of the array and its elements. ThenextInt()
method captures the size and each element.
Step 2: Separate Even and Odd Numbers
- The
separateEvenOdd()
method is used to rearrange the elements of the array so that all even numbers are placed before all odd numbers:- Two pointers,
left
andright
, are initialized to point to the start and end of the array, respectively. - The
left
pointer moves rightward as long as it points to an even number. - The
right
pointer moves leftward as long as it points to an odd number. - When
left
points to an odd number andright
points to an even number, the two elements are swapped. - The process continues until the
left
pointer crosses theright
pointer.
- Two pointers,
Step 3: Display the Result
- The program prints the array after separating the even and odd numbers using a
for
loop.
Output Example
Example 1:
Enter the size of the array: 6 Enter the elements of the array: 1 2 3 4 5 6 Array after separating even and odd numbers: 2 4 6 1 3 5
Example 2:
Enter the size of the array: 5 Enter the elements of the array: 10 9 8 7 6 Array after separating even and odd numbers: 10 6 8 7 9
Example 3:
Enter the size of the array: 4 Enter the elements of the array: 4 1 2 3 Array after separating even and odd numbers: 4 2 3 1
Conclusion
This Java program demonstrates how to separate even and odd numbers in an array. The program covers essential concepts such as array manipulation, loops, and conditional logic. This exercise is valuable for understanding how to efficiently rearrange elements within an array based on specific criteria in Java programming.