Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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. */
- import java.util.Locale;
- import java.util.Scanner;
- public class _03_MinMaxSumAndAverageOfN_Numbers {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Locale.setDefault(Locale.ROOT);
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter a whole positive number for Count of numbers in Sequence: ");
- int count = scanner.nextInt();
- if (count > 0) {
- int minNum = Integer.MAX_VALUE;
- int maxNum = Integer.MIN_VALUE;
- long sumNums = 0;
- for (int i = 0; i < count; i++) {
- System.out.print("Enter next Integer number of Sequence: ");
- int number = scanner.nextInt();
- sumNums += number;
- if (number < minNum) {
- minNum = number;
- }
- if (number > maxNum) {
- maxNum = number;
- }
- }
- scanner.close();
- System.out.printf("min = %d\nmax = %d\nsum = %d\n", minNum, maxNum, sumNums);
- System.out.printf("avg = %.2f\n", (sumNums / (double) count));
- } else {
- scanner.close();
- System.out.println("Error! - Invalid Input number!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement