Java find minimum value in array – Java Program to Find Minimum Value in Array by Using Recursion

Prerequisite: Recursion in Java

Find minimum value in array java: In the previous article, we have discussed about Java Program to Find Maximum Value in Array by Using Recursion

In this program we are going to find minimum value in array using recursion by Java programming language.

Java Program to Find Minimum Value in Array by Using Recursion

Java find minimum value in array: If any array element is smaller than all the array elements present in the array, then that element is the minimum element in the array.

For example-

If an array a[]={5, 8, 2, 9, 3}
Then the minimum element in array = 2

Now let’s see different ways to find minimum value in array by using recursion.

Method-1: Java Program to Find Minimum Value in Array By Using Static Input and Recursion

Approach:

  • Declare and initiate one array with some integer values.
  • Declare another integer variable in which the length of the array will be stored.
  • Define a user defined method find_Min() and pass arr[] and len as parameter.
  • Inside the user defined method find minimum number by traversing the total array by calling the same method recursively.
  • Print the result.

Program:

import java.util.*;
class Main 
{
    public static void main(String args[])
    {
        //Declare and initiate an array with some integer values
        int arr[] = {1,23,21,-8,45};
        int len = arr.length;
        //call the method
        System.out.println("Minimum value present in the array is: "+find_Min(arr, len));
    }
    
    public static int find_Min(int arr[], int len)
    {
        // if size = 0 means whole array has been traversed
        if(len == 1)
            return arr[0];
            return Math.min(arr[len-1], find_Min(arr, len-1));
    }
}
Output:

Minimum value present in the array is: -8

Method-2: Java Program to Find Minimum Value in Array By Using User Input and Recursion

Approach:

  • Declare an array with some integer values.
  • Declare another integer variable in which the length of the array will be stored.
  • Prompt the user to take input of the length value of array and array elements.
  • Define a user defined method find_Min() and pass arr[] and len as parameter.
  • Inside the user defined method find minimum number by traversing the total array by calling the same method recursively.
  • Print the result.

Program:

import java.lang.Math;
import java.util.Scanner;
class Main 
{
    public static void main(String args[])
    {
    	Scanner sc=new Scanner(System.in);
    	System.out.print("Enter the number of elements you want to store: ");
    	int n=sc.nextInt();
    	// Input array
    	int[] arr = new int[100];
    	System.out.print("Enter the elements of array: ");
    	for(int i=0;i<n;i++)
    	{
    		arr[i]=sc.nextInt();
    	}
    	int len = arr.length;
    	//call the method
    	System.out.println("Minimum value present in the array is: "+find_Min(arr, len));
    }
    
    public static int find_Min(int arr[], int len)
    {
        // if size = 0 means whole array has been traversed
        if(len == 1)
            return arr[0];   
        return Math.min(arr[len-1], find_Min(arr, len-1));
    }
}
Output:

Enter the number of elements you want to store: 5
Enter the elements of array: -10 21 67 -9 -1
Minimum value present in the array is: -10

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: