# Java Program to find the smallest number in an Array

In this post, we are going to see how to find the smallest number in an Array using core Java program.

So, here is the complete program for you (JavaSmallestInArray.java):

The output of the above program is:

Please go through inline comments in the above program to understand it. But the basic idea is, we are swapping the array values each time when there is the smallest number.

Here is a detailed explanation of the above program about the swapping,

//this is our array and the array length is 7
int arr[] = {10, 20, 101, 122, 1, 5, 12};

Let’s see the loop and if condition:

1. if (a < a) ie., if (10 < 20) – is true, so need to swap the value
• let’s swap, a = 20, a = 10
2. if (a < a) ie., if (10 < 101) – is true again, so, swap the value again
• let’s swap, a = 101, a = 10
3. if (a < a) ie., if (10 < 122) – is true again, so, swap the value again
• let’s swap, a = 122, a = 10
4. if (a < a) ie., if (10 < 1) – is false, so nothing happens here
5. if (a < a) ie., if (1 < 5) – is true again, so, swap the value again
• let’s swap, a = 5, a = 1
6. if (a < a) ie., if (1 < 12) – is true again, so, swap the value again
• let’s swap, a = 12, a = 1

So, the smallest number is at the end of the array arr = 1. That’s the value we are printing at the end.