Program to find the smallest number in an array

Input

NA. The array of numbers is already provided in the program

Program Code

package programs.java4newbie.com;

/*
* This Program finds the smallest number and prints it.
*/
public class SmallestNumber {

public static void main(String[] args) {

//array of 10 numbers
int numbers[] = new int[]{123,321,423,12,4,4444,567,7897,43,9};

//assign first element of an array to min
int min = numbers[0];

for(int i=1; i< numbers.length; i++)
{ // if current number is smaller than min, then this is the new min
if(numbers[i] < min)
min = numbers[i];

}

System.out.println("Smallest Number is : " + min);
}
}

Output

Smallest Number is : 4

Leave a comment