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);
   }
}

Output:

Largest number is : 98
Smallest number is : 23


Conclusion

That’s it guys. This is all about java program to find largest and smallest number in an array. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Related Posts