forked from Sanyam-7/Hacktoberfest2022-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
48 lines (36 loc) · 1.24 KB
/
Main.java
File metadata and controls
48 lines (36 loc) · 1.24 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
import java.util.Scanner;
public class Main {
private static double calculateAverage(int[] age) {
double sum = 0.0;
for (int i : age) {
sum += i;
}
return sum / (double) age.length;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter total no.of employees:");
int n = scanner.nextInt();
if (n < 2) {
System.out.println("Please enter a valid employee count");
} else {
System.out.println(String.format("Enter the age for %d employees:", n));
int[] age = new int[n];
boolean allValidAge = true;
for (int i = 0; i < n; ++i) {
int a = scanner.nextInt();
if (a < 28 || a > 40) {
allValidAge = false;
break;
}
age[i] = a;
}
if (allValidAge) {
double average = calculateAverage(age);
System.out.println(String.format("The average age is %.2f", average));
} else {
System.out.println("Invalid age encountered!");
}
}
}
}