-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArraySeperation.java
66 lines (43 loc) · 1.53 KB
/
ArraySeperation.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//write a java program to seprate odd and even integer into seprtate arrays without recurssion
import java.util.Scanner;
public class ArraySeperation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
arr[i] = sc.nextInt();
}
sc.close();
separateOddEven(arr);
}
public static void separateOddEven(int[] arr) {
int oddIndex = 0;
int evenIndex = 0;
int[] oddArr = new int[arr.length];
int[] evenArr = new int[arr.length];
for (int num : arr) {
if (num % 2 == 0) {
evenArr[evenIndex] = num;
evenIndex++;
}
else {
oddArr[oddIndex] = num;
oddIndex++;
}
}
System.out.println("The odd array is: ");
printArray(oddArr, oddIndex);
System.out.println("The even array is: ");
printArray(evenArr, evenIndex);
}
public static void printArray(int[] arr, int size) {
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}