JAVA Program to Calculate min & max values using Loops

// Calculate min & max values using Loops

public class IterativeMinMax {
   
   public static void main(String args[]) {
     
     int[] array = {20, 33, 10, 55, 30, 44, 40, 11, 50, 22};
     int min, max, l;
     
     min = max = array[0];
     l = array.length;
   
     for (int i=0; i max)
         max = array[i];
       if (array[i] < min)
         min = array[i];
     }
     System.out.println("This is the haystack: ");
     display(array);
     
     System.out.println("Max: " +max);
     System.out.println("Min: " +min);
   }
   
   private static void display(int x[]) {
     for (int i=0; i<x.length; i++)
       System.out.print(x[i] + " ");
     System.out.println("\n");
   }
   
}
Advertisements