Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class MinMaxSumAndAverageOfNNumbers {
- public static void main(String[] args) {
- /*Write a program that reads from the console a sequence of
- n integer numbers and returns the minimal, the maximal number,
- the sum and the average of all numbers (displayed with 2 digits after the decimal point).
- The input starts by the number n (alone in a line)
- followed by n lines, each holding an integer number.
- The output is like in the examples below.
- */
- Scanner sc = new Scanner(System.in);
- int n = Integer.parseInt(sc.nextLine());
- int[] inputs = new int[n];
- for (int i = 0; i < n; i++) {
- inputs[i] = Integer.parseInt(sc.nextLine());
- }
- // for (int i = 0; i < n; i++) {
- // if (i == n - 1) continue;
- int max = inputs[0];
- int min = inputs[0];
- int sum = inputs[0];
- double avg = Double.parseDouble(String.valueOf(inputs[0])); // /n
- for (int i = 0; i < n; i++) {
- min = Math.min(min, inputs[i]);
- max = Math.max(max, inputs[i]);
- sum = Integer.sum(sum, inputs[i]);
- avg = (Double.sum(sum, inputs[i])) / n;
- }
- System.out.println("min = " + min);
- System.out.println("max = " + max);
- System.out.println("sum = " + sum);
- System.out.printf("avg = %.2f", avg);
- }
- }
- /* Examples:
- n output
- 3
- 2
- 5
- 1 min = 1
- max = 5
- sum = 8
- avg = 2.66
- n output
- 2
- -1
- 4 min = -1
- max = 4
- sum = 3
- avg = 1.5
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement