In: Computer Science
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
import java.util.Scanner;
public class Statistics {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
int count=0,max=-1,n;
double sum=0,avg=0;
System.out.println("Enter integers
(negative integer to quit) : ");
while(true) {
//reading
integer
n=sc.nextInt();
//if it is
negative than break the loop
if(n<0)
break;
count++;
// if max is
less than current number than update max to current number
if(max
sum+=n;
}
//finding average
avg=sum/count;
System.out.println("Max number :
"+max);
System.out.println("Average:
"+avg);
}
}