Write a program in Java that finds the number of items above the average of all
ID: 3808187 • Letter: W
Question
Write a program in Java that finds the number of items above the average of all items. The problem is to read 100 numbers, get the average of these numbers, and find the number of the items greater than the average. To be flexible for handling any number of input, we will let the user enter the number of input, rather than fixing it to 100. Sample input: Enter the number of items: 10 Enter the numbers: 3.4 5 6 1 6.5 7.8 3.5 8.5 6.3 9.5 Sample output: Average is 5.75 Number of elements above the average is 6
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
int size; // size of array
Scanner sc = new Scanner(System.in); // scanner to take input from console
System.out.print(" Enter the number of items:");
size = sc.nextInt(); // get size;
double arr[] = new double[size]; // double array to hold numbers
double total = 0.0; // initialize total to zero
System.out.print(" Enter the numbers:");
for(int i =0; i<size; i++)
{
arr[i] = sc.nextDouble(); // get numbers
total+=arr[i]; // add it to total
}
double average =0.0;
if(size!=0) // condition to avoid divide by zero
average = total/size; // calculate average
int no_of_element_above_average = 0;
for(int i =0; i<size; i++)
{
if(arr[i]>average) // test condition
no_of_element_above_average++;
}
//print result
System.out.println("Average is "+average+" Number of elements above the average is "+no_of_element_above_average);
}
}
OUTPUT
OUTPUT2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.