Java find max in array

Hey guys!!! Welcome to flower brackets blog. In today’s post let us learn java find max in array.

java find max in array

Here, I’m gonna show you how to write a program to find largest and smallest number in an array in java.

Also read – convert decimal to octal java program

Program : find largest and smallest number in an array in java

public class LargestSmallest
   public static void main(String[] args)
      int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
      int largest = numbers[0];
      int smallest = numbers[0];

      for(int x = 1; x < numbers.length; x++)
         if(numbers[x] > largest)
            largest = numbers[x];
         else if(numbers[x] < smallest)
            smallest = numbers[x];
      System.out.println("Largest number is : " + largest);
      System.out.println("Smallest number is : " + smallest);


Largest number is : 98
Smallest number is : 23

Related Posts