Find Smallest Number in an Array Using Java
The following Java program prints the smallest number in a given array. This example uses an inline array, however it can be easily changed to a method taking an array as parameter. We loop through the array comparing whether the current smallest number is bigger than the array value. If yes, we replace the current smallest number with the array value. The initial value of the smallest number is set as Integer.MAX_VALUE which is the largest value an integer variable can have!
Find Smallest Number in an Array Using Java
// Java program to find smallest number in an array public class FindSmallest { public static void main(String[] args) { int[] numbers = {88,33,55,23,64,123}; int smallest = Integer.MAX_VALUE; for(int i =0;i<numbers.length;i++) { if(smallest > numbers[i]) { smallest = numbers[i]; } } System.out.println("Smallest number in array is : " +smallest); } }