Skip to content

Commit 3f85e52

Browse files
authored
Program to Count Even and Odd Numbers in an Array Using Java (#3563)
2 parents 63c79de + 6195881 commit 3f85e52

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

Java/EvenOddArray.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
import java.util.Scanner;
2+
23
public class EvenOddArray {
3-
public static void main (String[] args)
4-
{
5-
int n;
6-
Scanner sc = new Scanner(System.in);
7-
System.out.println("Enter the size of the array: ");
8-
n = sc.nextInt();
9-
int[]arr = new int[n];
10-
System.out.println("Enter the array elements: ");
11-
for(int i = 0; i < n; i++)
12-
{
13-
arr[i] = sc.nextInt();
14-
}
15-
int even = 0, odd = 0;
16-
for(int i=0; i<n; i++){
17-
if((arr[i] % 2 )== 0)
18-
even += 1;
19-
else
20-
odd += 1;
21-
}
22-
System.out.println("Number of even elements: "+ even);
23-
System.out.println("Number of odd elements: "+ odd);
24-
}
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in);
6+
7+
System.out.println("Enter the size of the array: ");
8+
int n = sc.nextInt();
9+
10+
int[] arr = new int[n];
11+
12+
System.out.println("Enter the array elements: ");
13+
for (int i = 0; i < n; i++) {
14+
arr[i] = sc.nextInt();
15+
}
16+
17+
int even = 0, odd = 0;
18+
19+
for (int i = 0; i < n; i++) {
20+
if (arr[i] % 2 == 0) {
21+
even++;
22+
} else {
23+
odd++;
24+
}
25+
}
26+
27+
System.out.println("Number of even elements: " + even);
28+
System.out.println("Number of odd elements: " + odd);
29+
30+
sc.close();
31+
}
2532
}

0 commit comments

Comments
 (0)