Ask Question
27 October, 05:50

g 4.16 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:

+1
Answers (1)
  1. 27 October, 06:56
    0
    import java. util. ArrayList;

    import java. util. Scanner;

    import java. util. Collections;

    public class num4 {

    public static void main (String[] args) {

    int sum = 0;

    Scanner in = new Scanner (System. in);

    System. out. println ("Enter number");

    int num = in. nextInt ();

    ArrayList list = new ArrayList ();

    while (num > = 0) {

    list. add (num);

    System. out. println ("Enter another positive number, enter a negative to stop");

    num = in. nextInt ();

    }

    int maximum = Collections. max (list);

    int listSize = list. size ();

    for (int i:list) {

    sum = sum+i;

    }

    double average = (double) sum/listSize;

    System. out. println ("Maximum number entered is "+maximum);

    System. out. println ("Average of the number is "+average);

    }

    }

    Explanation:

    In this program:

    A Scanner class is used to receive user input and store in a variable A while loop with the condition while (num > = 0) is used to ensure only non-negative numbers are entered An ArrayList is created and used to store all the user's valid entries The Max Method in the Collections class is used to find the maximum value entered To find the average, a for loop is used to add all elements in the list and divide by the size of the list Finally the Average and maximum is printed out
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “g 4.16 Write a program that takes any number of non-negative integers as input, and outputs the average and max. A negative integer ends ...” 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