Find Smallest Number in Array Java

Find Smallest Number in Array Java | Array Programs in Java – 13 | In the previous Java program, we developed programs to find the largest number in a given array using linear search. Now, let us discuss how to find the smallest number in array Java. Or, how to find min value in array Java?

Program description:- Write a Java program to find the smallest number in Java.

Procedure to develop the method to find the smallest number in array Java,

a) Take a one-dimensional array (assume array variable as arr)
b) Declare a variable min
c) Assign the first element of the array to the smallest variable i.e. min = arr[0]
d) Iterate through all elements of the array using the loop
e) Check the ith element in the array is lesser than min?
f) If yes, then update min variable i.e. assign the ith element to the min
g) Else, go to the next step
h) Repeat (e) to (g) until the end of the array.
i) Return min

Java Method to Find Smallest Number in Array Java

// Java method to find smallest number in array
public static int smallest(int[] arr) {

   // declare a variable min
   int min = 0;

   // assign first element to min
   min = arr[0];

   // compare with remaining elements
   // loop
   for (int i = 1; i < arr.length; i++) {
      // compare
      if (min > arr[i])
      min = arr[i];
   }

   // return result
   return min;
}

At the start, we assume that the first element of the array is the smallest number and then compare it with the remaining elements. If any element is lesser than it, then assume that number as the smallest number. At last, we will get the smallest number in the given array.

At starting we should not continue with min = 0 because the array may have positive numbers and since 0 is always lesser than positive numbers, therefore, we may get the result as the smallest number = 0.

Using the above method let us develop the Java program. We will take array input from the end-user, first, we will ask to enter the size of the array and then enter the array elements. To take input from the end-user we will use the Scanner class, but you can also use the BufferedReader class. After finding the smallest number in the given array we will display the result.

Program to Find Smallest Number in Array Java

import java.util.Scanner;

public class ArrayProgram {

  // Java method to find smallest number in array
  public static int smallest(int[] arr) {
    
    int min = 0;
    min = arr[0];

    for (int i = 1; i < arr.length; i++) {
      if (min > arr[i])
        min = arr[i];
    }

    return min;
  }
  
  // main method
  public static void main(String[] args) {
    // create Scanner class object to read input
    Scanner scan = new Scanner(System.in);

    // declare variables
    int length = 0;
    int numbers[] = null;

    // take length of the array
    System.out.print("Enter length of the array: ");
    length = scan.nextInt();

    // create array
    numbers = new int[length];

    // take array inputs
    System.out.println("Enter array elements: ");
    for (int i = 0; i < numbers.length; i++) {
      numbers[i] = scan.nextInt();
    }

    // method call
    System.out.println("Smallest element = " 
                          + smallest(numbers));

    // close Scanner
    scan.close();
  }
}

Output for the different test-cases:-

Enter length of the array: 5
Enter array elements:
25 40 10 17 45
Smallest element = 10

Enter length of the array: 5
Enter array elements:
10 -20 0 -50 90
Smallest element = -50

In this Java program, first, we created a Scanner class object to get input values from the end-user. Then, the length of the array and array elements are aksed from the end-user and stored into appropriate variables. On this array, we called the smallest() method.

The smallest() method is given to find smallest number in array Java. Based on the previous code it finds and returns the smallest element among the array.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *