Java – Find Smallest Number of an Array
A number array like integer array, float array, double array or long array can contain numbers with different values. We can find the smallest number of these, in an array.
In this tutorial, we shall learn how to find the smallest number of a given array using different looping statements in Java.
Example 1 – Find Smallest Number of Array using While Loop
In this example, we shall use Java While Loop, to find smallest number of given integer array.
Solution
- Take an integer array with some elements.
- Initialize a variable
smallest
with the greatest value an integer variable can hold,Integer.MAX_VALUE
. This ensures that thesmallest
picks the first element of the given array, in first iteration of the loop. - Take a variable
index
. We shall use it to access elements of the array. Initializeindex
with zero. We shall start from the left of array. - Write a while loop that executes when
index
is less than length of the integer array. Incrementindex
during each iteration. - Inside while loop, check if
smallest
is greater than this element. If so, then update thesmallest
with this element. Ifsmallest
is not greater than this element, we are happy with existing value ofsmallest
, nothing to do, so no else block required. - After the while loop execution is done, we end with the smallest element of array, in the variable
smallest
.
Example.java
/**
* Java Program - Find Smallest Number of an Array
*/
public class Example {
public static void main(String[] args) {
//an array
int[] arr = {25, 86, 41, 97, 22, 34};
//initialize with largest possible value
int smallest = Integer.MAX_VALUE;
//find smallest element of array
int index=0;
while(index<arr.length) {
//check if smallest is greater than element
if(smallest>arr[index]) {
//update smallest
smallest=arr[index];
}
index++;
}
System.out.println("The smallest number is : "+ smallest);
}
}
Run the above Java program in your IDE or using Java command in command prompt. You shall get the following output in console.
Output
The smallest number is : 22
The program found out the smallest integer in given integer array, as shown in the output.
Example 2 – Find Smallest Number of Array using For Loop
In our previous example, we have taken an integer array. So, In this example, we shall take a float array and find smallest floating point number using Java For Loop.
Solution
- Take a floating point array with some elements.
- Initialize a variable
smallest
with the largest of the Float value,Float.MAX_VALUE
. This ensures that thesmallest
picks the first element of the given array, in first iteration of the loop. - Take a variable
index
. We shall use it to access elements of the array. Initializeindex
with zero in For Loop initialization part. We shall start from the left of array. - Write a for loop that executes when
index
is less than length of the integer array. Incrementindex
during each iteration. - Inside for loop, check if
smallest
is greater than this element. If so, then update thesmallest
with this element. Ifsmallest
is not greater than this element, we are happy with existing value ofsmallest
, nothing to do, so no else block required. - After the for loop execution is done, we end with the smallest element of array, in the variable
smallest
.
Example.java
/**
* Java Program - Find Smallest Number of an Array
*/
public class Example {
public static void main(String[] args) {
//an array
float[] arr = {2.5f, 6.9f, 4.1f, 9.7f, 2.2f, 3.4f};
//initialize with largest possible value
float smallest = Float.MAX_VALUE;
//find smallest element of array
for(int index=0; index<arr.length; index++) {
//check if smallest is greater than element
if(smallest>arr[index]) {
//update smallest
smallest=arr[index];
}
}
System.out.println("The smallest number is : "+ smallest);
}
}
Output
Run the above Java Program in your IDE or command prompt using Java command.
The smallest number is : 2.2
The program found the smallest floating point number in given floating point array as shown in the output.
Example 3 – Find Smallest Number of Array using Advanced For Loop
In this example, we shall take a double array and find the smallest number using Java Advanced For Loop.
Solution
- Take a double array with some elements.
- Initialize a variable
smallest
with the largest of the Double value,Double.MAX_VALUE
. This ensures that thesmallest
picks the first element of the given array, in first iteration of the loop. - Write an advanced for loop that iterates over each element of the double array.
- Inside for loop, check if
smallest
is greater than this element. If so, then update thesmallest
with this element. Ifsmallest
is not greater than this element, we are happy with existing value ofsmallest
, nothing to do, so no else block required. - After the advanced for loop execution is done, we end with the smallest element of array, in the variable
smallest
.
Example.java
/**
* Java Program - Find Smallest Number of an Array
*/
public class Example {
public static void main(String[] args) {
//an array
double[] arr = {2.5, 6.9, 4.1, 9.7, 2.2, 3.4};
//initialize with largest possible value
double smallest = Double.MAX_VALUE;
//find smallest element of array
for(double element : arr) {
//check if smallest is greater than element
if(smallest>element) {
//update smallest
smallest=element;
}
}
System.out.println("The smallest number is : "+ smallest);
}
}
Output
Run the above Java Program in your IDE or command prompt using Java command.
The smallest number is : 2.2
The program found the smallest double in given double array as shown in the output.
Conclusion
In this Java Tutorial, we learned how to find the smallest number of a given array, using different looping statements in Java.