-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySegregate.java
More file actions
39 lines (34 loc) · 990 Bytes
/
Copy pathArraySegregate.java
File metadata and controls
39 lines (34 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class ArraySegregate {
public static void main(String[] args) {
int a[] = { 10, 53, 22, 21, 86, 92 };
int ecnt = 0;
int ocnt = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0)
ecnt++;
else
ocnt++;
}
int even[] = new int[ecnt];
int odd[] = new int[ocnt];
int k = 0, t = 0;
for (int i = 0; i < ecnt; i++) {
if (a[i] % 2 == 0) {
even[k] = a[i];
k++;
} else {
odd[t] = a[i];
t++;
}
}
System.out.println("The array with even elements = ");
for (int i = 0; i < ecnt; i++) {
System.out.print(even[i] + " ");
}
System.out.println("");
System.out.println("The array with odd elements = ");
for (int i = 0; i < ocnt; i++) {
System.out.print(odd[i] + " ");
}
}
}