-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAnalyzeNumbers.java
35 lines (29 loc) · 917 Bytes
/
AnalyzeNumbers.java
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
package chapter_seven.samples;
/**
* Listing 7.1 AnalyzeNumbers.java
*/
public class AnalyzeNumbers
{
public static void main(String[] args)
{
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the number of items: ");
int n = input.nextInt();
double[] numbers = new double[n];
double sum = 0;
System.out.print("Enter the numbers: ");
for (int i = 0; i < n; i++)
{
numbers[i] = input.nextDouble();
sum += numbers[i];
}
double average = sum / n;
int count = 0; // The number of elements above average
for (int i = 0; i < n; i++)
if (numbers[i] > average)
count++;
System.out.println("Average is " + average);
System.out.println("Number of elements above the average is "
+ count);
}
}