Google News
logo
Java program to find Smallest Number in an Array
In the following example of Java program to find the smallest number in an array :
Program :
public class SmallestNumber {
    public static void main(String[] args) {
        int[] arr = {12, 31, 25, 17, 40, 9, 34};
        int min = arr[0];
        
        for(int i=1; i<arr.length; i++){
            if(arr[i] < min){
                min = arr[i];
            }
        }
        
        System.out.println("Smallest number in the array: " + min);
    }
}
Output :
Smallest number in the array: 9
In this program, we have an integer array arr. We initialize a variable min with the first element of the array. Then, we loop through the rest of the array and compare each element with min.

If the current element is smaller than min, we update the value of min. Finally, we print the value of min, which is the smallest element of the array.