import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class MinMax{
public static void main(String[] args)
{
// Creates an array of integer numbers in it.
System.out.println("Enter Up To 10 Numbers");
Scanner sc=new Scanner(System.in);
Integer[] numbers =new Integer[10];
// Reading array elements dynamically using scanner class
for (int i=0;i<numbers.length;i++)
{
System.out.print("enter numbers["+i+"]:");
numbers[i]=sc.nextInt();
}
// use the Collections.min() and Collections.max() methods.
// But as this method requires a list type of data we need
// to convert the array to list first.
int min = (int) Collections.min(Arrays.asList(numbers));
int max = (int) Collections.max(Arrays.asList(numbers));
// we get the minimum and the maximum value from the array.
System.out.println("Min number: " + min);
System.out.println("Max number: " + max);
}
}
OUTPUT:
Enter Up To 10 Numbers enter numbers[0]:10 enter numbers[1]:56 enter numbers[2]:48 enter numbers[3]:79 enter numbers[4]:89 enter numbers[5]:87 enter numbers[6]:98 enter numbers[7]:55 enter numbers[8]:24 enter numbers[9]:37 Min number: 10 Max number: 98