Advertisement
Spocoman

05. Average Number

Aug 29th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class AverageNumber {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int numberCount = Integer.parseInt(scanner.next()),
  7.                 number, sumNumbers = 0;
  8.  
  9.         for (int i = 0; i < numberCount; i++) {
  10.             number = Integer.parseInt(scanner.next());
  11.             sumNumbers += number;
  12.         }
  13.  
  14.         double average = 1.0 * sumNumbers / numberCount;
  15.         System.out.printf("%.2f\n", average);
  16.     }
  17. }
  18.  
  19. ИЛИ:
  20.  
  21. import java.util.Scanner;
  22.  
  23. public class Main {
  24.     public static void main(String[] args) {
  25.         Scanner scanner = new Scanner(System.in);
  26.         int numberCount = Integer.parseInt(scanner.next()), sumNumbers = 0;
  27.  
  28.         for (int i = 0; i < numberCount; i++) {
  29.             sumNumbers += Integer.parseInt(scanner.next());
  30.         }
  31.  
  32.         System.out.printf("%.2f\n", (double) sumNumbers / numberCount);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement