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

  1. Take an integer array with some elements.
  2. Initialize a variable smallest with the greatest value an integer variable can hold, Integer.MAX_VALUE. This ensures that the smallest picks the first element of the given array, in first iteration of the loop.
  3. Take a variable index. We shall use it to access elements of the array. Initialize index with zero. We shall start from the left of array.
  4. Write a while loop that executes when index is less than length of the integer array. Increment index during each iteration.
  5. Inside while loop, check if smallest is greater than this element. If so, then update the smallest with this element. If smallest is not greater than this element, we are happy with existing value of smallest, nothing to do, so no else block required.
  6. 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.

ADVERTISEMENT

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

  1. Take a floating point array with some elements.
  2. Initialize a variable smallest with the largest of the Float value, Float.MAX_VALUE. This ensures that the smallest picks the first element of the given array, in first iteration of the loop.
  3. Take a variable index. We shall use it to access elements of the array. Initialize index with zero in For Loop initialization part. We shall start from the left of array.
  4. Write a for loop that executes when index is less than length of the integer array. Increment index during each iteration.
  5. Inside for loop, check if smallest is greater than this element. If so, then update the smallest with this element. If smallest is not greater than this element, we are happy with existing value of smallest, nothing to do, so no else block required.
  6. 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

  1. Take a double array with some elements.
  2. Initialize a variable smallest with the largest of the Double value, Double.MAX_VALUE. This ensures that the smallest picks the first element of the given array, in first iteration of the loop.
  3. Write an advanced for loop that iterates over each element of the double array.
  4. Inside for loop, check if smallest is greater than this element. If so, then update the smallest with this element. If smallest is not greater than this element, we are happy with existing value of smallest, nothing to do, so no else block required.
  5. 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.