-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_Even_Odd_Array.java
More file actions
32 lines (26 loc) · 1.06 KB
/
Copy pathCount_Even_Odd_Array.java
File metadata and controls
32 lines (26 loc) · 1.06 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
import java.util.*;
import java.util.*;
public class Count_Even_Odd_Array {
public static void main(String[] args) {
// Initialize an array of integers
int[] nums = {5, 7, 2, 4, 9};
// Initialize counters for even and odd numbers
int ctr_even = 0, ctr_odd = 0;
// Display the original array
System.out.println("Original Array: " + Arrays.toString(nums));
// Iterate through the array to count even and odd numbers
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 0) {
// Increment the counter for even numbers
ctr_even++;
} else {
// Increment the counter for odd numbers
ctr_odd++;
}
}
// Print the counts of even and odd elements in the array
System.out.printf("\nNumber of even elements in the array: %d", ctr_even);
System.out.printf("\nNumber of odd elements in the array: %d", ctr_odd);
System.out.printf("\n");
}
}