Skip to content

Commit 4dddb42

Browse files
committed
feat: Identify and print even numbers from a user-defined array
🧠 Logic: - Prompt the user for array size and initialize the array accordingly. - Accept array elements using a loop. - Traverse the array and print elements that are divisible by 2 (even numbers). - Closes the Scanner after input to prevent resource leak. Signed-off-by: Somesh diwan <[email protected]>
1 parent 8398fa2 commit 4dddb42

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import java.util.Scanner;
2-
class Size
3-
{
4-
public static void main (String []args)
5-
{
2+
3+
class Size {
4+
public static void main(String[] args) {
65
Scanner sc = new Scanner(System.in);
76
int n;
8-
System.out.println("enter the size of array");
7+
8+
System.out.println("Enter the size of array");
99
n = sc.nextInt();
10-
int[]A = new int [n];
11-
System.out.println("enter the size of array");
12-
for (int i = 0; i <A.length; i++)
13-
{
14-
if (A[i]%2 == 0)
15-
{
10+
11+
int[] A = new int[n];
12+
System.out.println("Enter the elements of array");
13+
// Read array elements
14+
for (int i = 0; i < A.length; i++) {
15+
A[i] = sc.nextInt();
16+
}
17+
18+
System.out.println("Even numbers in the array:");
19+
for (int i = 0; i < A.length; i++) {
20+
if (A[i] % 2 == 0) {
1621
System.out.println(A[i]);
1722
}
1823
}
24+
sc.close(); // Close the scanner when done
1925
}
20-
}
26+
}

0 commit comments

Comments
 (0)