Terondai

Untitled

Sep 14th, 2021 (edited)
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.math.BigDecimal;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.   private static final double BigDecimal = 0;
  6.  
  7.   public static void main(String[] args)
  8.   {
  9.    
  10.     Scanner sc = new Scanner(System.in);
  11.     int n = sc.nextInt();
  12.     double arr[] = new double[n];
  13.    
  14.     // Set smallest and largest values.
  15.  
  16.     double smallest = Double.MAX_VALUE;
  17.     double largest = Double.MIN_VALUE; // -100000;
  18.     double sum = 0;
  19.     double average = 0;
  20.  
  21.     // Get the input in an array.
  22.     for (int i = 0; i < n; i++)
  23.     {
  24.       arr[i] = sc.nextDouble();
  25.     }
  26.  
  27.     //Check what is inside the array
  28.     //System.out.println(Arrays.toString(arr));
  29.  
  30.  
  31.     for (int i = 0; i < arr.length; i++)
  32.     {
  33.       // If the current number is less than "smallest" make it the smallest.
  34.       if (arr[i] < smallest)
  35.       {
  36.         smallest = arr[i];
  37.       }
  38.  
  39.       // If the current number is bigger than than "largest" make it the largest.
  40.       if (arr[i] > largest)
  41.       {
  42.         largest = arr[i];
  43.       }
  44.      
  45.       sum = sum + arr[i];
  46.  
  47.     }
  48.       average = sum / arr.length;
  49.  
  50.     // If the largest number is less than zero make the smallest the largest;
  51.  
  52.     if ( largest == BigDecimal)
  53.     {
  54.       largest = smallest;
  55.     }
  56.  
  57.         System.out.printf("min=%.2f%n", smallest);
  58.         System.out.printf("max=%.2f%n", largest);
  59.         System.out.printf("sum=%.2f%n", sum);
  60.         System.out.printf("avg=%.2f%n", average);
  61.  
  62.     sc.close();
  63.  
  64.   }
  65. }
  66. /*
  67. Input   Output
  68. 3
  69.  
  70. 2
  71. 5
  72. 1  
  73.  
  74. min=1.00
  75. max=5.00
  76. sum=8.00
  77. avg=2.67
  78. 3
  79.  
  80. 2
  81. -1
  82. 4  
  83. min=-1.00
  84. max=4.00
  85. sum=5.00
  86. avg=1.67
  87.  
  88. */
RAW Paste Data Copied