Partition Array into Even and Odd
This program partitions an array into even and odd numbers.
Problem Statement
Write a Java program to partition an integer array into two arrays: one containing even numbers and the other containing odd numbers.
Source Code
| 1 | public class PartitionEvenOdd { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Partition Even and Odd"); |
| 4 | int[] arr = {1,2,3,4,5,6}; |
| 5 | String even="", odd=""; |
| 6 | for(int num:arr){ |
| 7 | if(num%2==0) even+=num+" "; |
| 8 | else odd+=num+" "; |
| 9 | } |
| 10 | System.out.println("Even partition: "+even); |
| 11 | System.out.println("Odd partition: "+odd); |
| 12 | } |
| 13 | } |
Program Output
Eduinq Partition Even and Odd Even partition: 2 4 6 Odd partition: 1 3 5
Explanation
A loop iterates through the array checking each number's parity using the modulus operator, appending it to either the even or odd string accordingly, demonstrating a simple partitioning logic on arrays.