-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab4.java
More file actions
73 lines (62 loc) · 2.1 KB
/
Lab4.java
File metadata and controls
73 lines (62 loc) · 2.1 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.*;
public class Lab4 {
void method() {
int x, i, choice, n = 0;
int sum = 0, largest = 0, smallest = 0;
Scanner st = new Scanner(System.in);
System.out.println("Enter N number:");
n = st.nextInt();
int a[] = new int[n];
System.out.println("Enter numbers one by one:");
for (i = 0; i < n; i++) {
a[i] = st.nextInt();
}
System.out.println("\nMenu:");
System.out.println("1. Sum of even numbers");
System.out.println("2. Largest number");
System.out.println("3. Smallest number");
System.out.println("4. Exit");
System.out.println("Enter your choice (1/2/3/4):");
choice = st.nextInt();
st.close();
switch (choice) {
case 1:
for (i = 0; i < n; i++) {
if (a[i] % 2 == 0) {
sum += a[i];
}
}
System.out.println("Sum of even numbers: " + sum);
break;
case 2:
for (i = 0; i < n; i++) {
if (a[i] > largest) {
largest = a[i];
}
}
System.out.println("Largest number is: " + largest);
break;
case 3:
smallest = a[0];
for (i = 0; i < n; i++) {
if (a[i] < smallest) {
smallest = a[i];
}
}
System.out.println("Smallest number is: " + smallest);
break;
case 4:
System.out.println("Exiting the program...");
break;
default:
System.out.println("Invalid choice!");
break;
}
}
}
class Exp {
public static void main(String[] args) {
Lab4 ex = new Lab4();
ex.method();
}
}