1// Java program to find maximum and minimum number without using array
2import java.util.Scanner;
3public class MaximumMinimumWithoutArray
4{
5 public static void main(String[] args)
6 {
7 Scanner sc = new Scanner(System.in);
8 System.out.println("Please enter total number: ");
9 int numbers = sc.nextInt();
10 int maximum = Integer.MIN_VALUE;
11 int minimum = Integer.MAX_VALUE;
12 System.out.println("Please enter " + numbers + " numbers.");
13 for(int a = 0; a < numbers; a++)
14 {
15 int current = sc.nextInt();
16 if(current > maximum)
17 {
18 maximum = current;
19 }
20 if(current < minimum)
21 {
22 minimum = current;
23 }
24 }
25 System.out.println("largest of " + numbers + " numbers is: " + maximum);
26 System.out.println("smallest of " + numbers + " numbers is: " + minimum);
27 sc.close();
28 }
29}
1private static int findMin(int[] array) {
2 int min = array[0];
3 for(int i=1;i<array.length;i++) {
4 if(min > array[i]) {
5 min = array[i];
6 }
7 }
8 return min;
9 }
1// Find minimum and maximum values in a java array
2public class MaxMinValueInArray
3{
4 public static void main(String[] args)
5 {
6 int[] arrNumber = new int[]{50, 150, 250, 500, 160, 530};
7 int maximum = maxNumber(arrNumber);
8 System.out.println("Maximum value : " + maximum);
9 // calling min function java
10 int minimum = minNumber(arrNumber);
11 System.out.println("Minimum value : " + minimum);
12 }
13 // java find max in array
14 public static int maxNumber(int[] arr)
15 {
16 int maximumValue = arr[0];
17 for(int a = 1; a < arr.length; a++)
18 {
19 if(arr[a] > maximumValue)
20 {
21 maximumValue = arr[a];
22 }
23 }
24 return maximumValue;
25 }
26 // java min of array
27 public static int minNumber(int[] arr)
28 {
29 int minimumValue = arr[0];
30 // find minimum value in array java
31 for(int a = 1; a < arr.length; a++)
32 {
33 if(arr[a] < minimumValue)
34 {
35 minimumValue = arr[a];
36 }
37 }
38 return minimumValue;
39 }
40}
1public class MinimumNumber{
2 public static void main(String[] args){
3 int[] arr=new int[]{2,3,4,1};
4 int min=arr[0];
5 for(int i=0;i<arr.length;i++){
6 if(arr[i]<min){
7 min=arr[i];
8 }
9 }
10 System.out.println("Minimum Number in array:"+min);
11 }
12}
1Integer[] num = { 2, 4, 7, 5, 9 };
2 // using Collections.min() to find minimum element
3 // using only 1 line.
4 int min1 = Collections.min(Arrays.asList(num));
5
6 // using Collections.max() to find maximum element
7 // using only 1 line.
8 int max1 = Collections.max(Arrays.asList(num));
9
10 System.out.println(min1); // 2
11 System.out.println(max1); // 9