Find Smallest Number in Array Java

Find the smallest Number in Array Java | 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 Java Array,
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 the Smallest Number in an Array

// 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 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.

Java Program to Find the Smallest Number in Array

import java.util.Scanner;

public class ArrayProgram {

  // 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++) {
      if (min > arr[i])
        min = arr[i];
    }

    // return result
    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

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

Leave a Reply