Ask Question
5 February, 15:46

g Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the average and max. A negative integer ends the input and is not included in the statistics. Ex: When the input is:

+3
Answers (1)
  1. 5 February, 17:39
    0
    import java. util. Scanner;

    public class LabProgram

    {

    public static void main (String[] args) {

    Scanner sc = new Scanner (System. in); //to read input

    int count=0; //to keep track of the count of numbers entered

    int max=0; //to store the maximum value

    int sum=0; //to store the sum of numbers entered

    double av=0; //to calculate and store the average

    //reading inputs until a negative number is entered

    while (true)

    {

    int n = sc. nextInt (); //reading input

    if (n<0) / /if negative number

    break; //then stopping loop

    count++; //increasing count

    if (count==1) / /means it is first number

    max=n;

    else if (max
    max=n; //updating max

    sum+=n; //adding new number to sum

    }

    //finding average

    av = (double) sum/count;

    //displaying output

    System. out. println ((int) av+" "+max); //remove type casting (int) here, if you want decimal places also

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “g Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers