Repository navigation
Expand file tree
/
Copy pathQ34.java
More file actions
56 lines (55 loc) · 1.6 KB
/
Copy pathQ34.java
File metadata and controls
56 lines (55 loc) · 1.6 KB
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
import java.util.Scanner;
public class Q34
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int size = scanner.nextInt();
int arr[] = new int[size];
int i;//Loop control variable
System.out.println("Please enter the array elements: ");
for(i = 0; i < arr.length; i++)
{
arr[i] = scanner.nextInt();
}
int countEven = 0, countOdd = 0;//Keeps count of even and odd elements
for(i = 0; i < arr.length; i++)
{
if (arr[i] % 2 == 0)
{
countEven++;
}
else
{
countOdd++;
}
}
int even[] = new int[countEven];
int odd[] = new int[countOdd];
int pos1 = 0, pos2 = 0;
for (i = 0; i < arr.length; i++)
{
if (arr[i] % 2 == 0)
{
even[pos1] = arr[i];
pos1++;
}
else
{
odd[pos2] = arr[i];
pos2++;
}
}
System.out.println("Separating given array elements into odd and even:");
System.out.println("Even elements are: ");
for (i = 0; i < countEven; i++)
{
System.out.print(even[i] + " ");
}
System.out.println("Odd elements are: ");
for (i = 0; i < countOdd; i++)
{
System.out.print(odd[i] + " ");
}
}
}