Java array find the largest and smallest number using for loop

Description

Java array find the largest and smallest number using for loop


import java.util.Arrays;
import java.util.Random;

public class Main {

  public static void main(String[] args) {
    int[] a = new int[10];
    /*  w ww .jav  a  2 s.c  om*/
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;

    for (int i = 0; i < a.length; i++) {
      a[i] = new Random().nextInt(100);
    }
    System.out.println(Arrays.toString(a));
    
    for (int i = 0; i < a.length; i++) {
      if (a[i] < min)
        min = a[i];
      if (a[i] > max)
        max = a[i];
    }
    System.out.println("Min is: " + min + " " + "Max is: " + max);
  }
}



PreviousNext

Related