-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCitizenOlderYounger.java
More file actions
41 lines (35 loc) · 1.26 KB
/
Copy pathCitizenOlderYounger.java
File metadata and controls
41 lines (35 loc) · 1.26 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
import java.util.Scanner;
public class CitizenOlderYounger {
public static void main(String[] args) {
int[] ages = new int[100];
//create object of Scanner class to take input from user
Scanner in = new Scanner(System.in);
//taking 100 citizens age
for (int i = 0; i < 100; i++) {
System.out.println("Enter age of " + (i + 1) + " citizen");
ages[i] = in.nextInt();
}
//sorting and calculate average age;
int totalAge = 0;
for (int i = 0; i < 100; i++) {
for (int j = i + 1; j < 100; j++) {
if (ages[i] < ages[j]) {
int temp = ages[i];
ages[i] = ages[j];
ages[j] = temp;
}
}
totalAge += ages[i];
}
double averageAge = totalAge / 100;
int count = 0;
for (int i = 0; i < 100; i++) {
System.out.println(ages[i]);
if (ages[i] > averageAge) {
count++;
}
}
System.out.println("Average age: "+averageAge);
System.out.println("total no of citizen whose age is greater than or equals to average age: "+count);
}
}