0% found this document useful (1 vote)
1K views

Lab 4.13 (JAVA)

4.13 LAB: Varied amount of input data 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.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
1K views

Lab 4.13 (JAVA)

4.13 LAB: Varied amount of input data 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.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

4.

13 LAB: Varied amount of input data


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: 15 20 0 5 -1


the output is: 10 20

You can assume that at least one non-negative integer is input.

CODE (JAVA):

import java.util.Scanner;

public class LabProgram {


public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int count = 0, max = 0, total = 0;

int num = scnr.nextInt();


while (num >= 0) {
count++;
total += num;
max = Math.max(max, num);
num = scnr.nextInt();
}

int avg = count == 0 ? 0 : total/count;


System.out.println(avg + " " + max);
}
}

You might also like