Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.util.Scanner;
- public class Main {
- private static final double BigDecimal = 0;
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- double arr[] = new double[n];
- // Set smallest and largest values.
- double smallest = Double.MAX_VALUE;
- double largest = Double.MIN_VALUE; // -100000;
- double sum = 0;
- double average = 0;
- // Get the input in an array.
- for (int i = 0; i < n; i++)
- {
- arr[i] = sc.nextDouble();
- }
- //Check what is inside the array
- //System.out.println(Arrays.toString(arr));
- for (int i = 0; i < arr.length; i++)
- {
- // If the current number is less than "smallest" make it the smallest.
- if (arr[i] < smallest)
- {
- smallest = arr[i];
- }
- // If the current number is bigger than than "largest" make it the largest.
- if (arr[i] > largest)
- {
- largest = arr[i];
- }
- sum = sum + arr[i];
- }
- average = sum / arr.length;
- // If the largest number is less than zero make the smallest the largest;
- if ( largest == BigDecimal)
- {
- largest = smallest;
- }
- System.out.printf("min=%.2f%n", smallest);
- System.out.printf("max=%.2f%n", largest);
- System.out.printf("sum=%.2f%n", sum);
- System.out.printf("avg=%.2f%n", average);
- sc.close();
- }
- }
- /*
- Input Output
- 3
- 2
- 5
- 1
- min=1.00
- max=5.00
- sum=8.00
- avg=2.67
- 3
- 2
- -1
- 4
- min=-1.00
- max=4.00
- sum=5.00
- avg=1.67
- */
Add Comment
Please, Sign In to add comment