Find minimum and maximum values in a java array

Let’s learn to find minimum and maximum values in a java array.

Find minimum and maximum values in a java array

To find minimum and maximum values in an array first we’re going to create two static methods.

One to find minimum value in an array and another to find maximum value in an array.

Also read – bubble sort in java

Here’s the java program to find maximum and minimum number in an array,

public class CalculateAverage 
{
   public static void main(String[] args) 
   {
      int[] arrNumber = new int[]{50, 150, 250, 500, 160, 530};
      int maximum = maxNumber(arrNumber);
      System.out.println("Maximum value : " + maximum);

      // calling min function java
      int minimum = minNumber(arrNumber);
      System.out.println("Minimum value : " + minimum);
   }

   // java find max in array
   public static int maxNumber(int[] arr)
   {
      int maximumValue = arr[0];
      for(int a = 1; a < arr.length; a++)
      {
         if(arr[a] > maximumValue)
         {
            maximumValue = arr[a];
         }
      }
      return maximumValue;
   }

   // java min of array
   public static int minNumber(int[] arr)
   {
      int minimumValue = arr[0];
      // find minimum value in array java
      for(int a = 1; a < arr.length; a++)
      {
         if(arr[a] < minimumValue)
         {
            minimumValue = arr[a];
         }
      }
      return minimumValue;
   }
}



Output:

find minimum and maximum values in a java array

Java program to find maximum and minimum number without using array

Here’s the example using java math.max array and java math.min array,

import java.util.Scanner;

public class MaximumMinimumWithoutArray 
{
   public static void main(String[] args) 
   {
      double num1, num2, num3, max, min;
      Scanner sc = new Scanner(System.in); 
      System.out.print("Please enter first number: " ); 
      num1 = sc.nextDouble() ;
      System.out.print("Please enter second number: " ); 
      num2 = sc.nextDouble() ;
      System.out.print("Please enter third number: " ); 
      num3 = sc.nextDouble();
      max = Math.min(num1, Math.min(num2, num3));
      min = Math.max(num1, Math.max(num2, num3));
      System.out.println("\n" + "Minimum number is " + max + "\n" + "Maximum number is " + min);
      sc.close();
   }
}


Output:

Please enter first number: 659
Please enter second number: 586
Please enter third number: 568

Minimum number is 568.0
Maximum number is 659.0


Find minimum value in array recursion java

Here’s the program on finding minimum value in array using recursion.

public class MinimumValueUsingRecursion 
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = {28, 18, 29, 38, 98, 27, 80}; 
      int num = arrNumbers.length;
      // here we are calling function 
      System.out.println(findMinimumValue(arrNumbers, num));
   }

   public static int findMinimumValue(int[] arr, int number) 
   {
      if(number == 1) 
         return arr[0];
      return Math.min(arr[number - 1], findMinimumValue(arr, number - 1)); 
   }
}


Output:

18

Find index of max value in array java

To find index of maximum value or largest number in an array first assign given numbers to an integer array “arrNumbers”.

Then using for loop iterate the given array and check maximum value or largest number is smaller than given value.

If the result is true reassign maximum value and index position. Finally print the index position on the console,

public class IndexOfMaxValue
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = new int[] { 26, 93, 58, 38, 90, 69, 29 };
      int maxValue = arrNumbers[0];
      int index = 0;
      for(int a = 0; a < arrNumbers.length; a++) 
      {
         if(maxValue < arrNumbers[a]) 
         {
            maxValue = arrNumbers[a];
            index = a;
         }
      }
      System.out.println("Index of max value in array java:  " + index);
   }
}


Output:

Index of max value in array java: 1