In this java program, I will share Java program to find the smallest number in an Array with an example. We have sorted the given array in ascending order and returning the first element. See the below example of finding the smallest number in java array using the Java programming language.
Copy the following Java program and execute it with the help of Java compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class SmallestInArrayExample{ public static int getSmallest(int[] a, int total){ int temp; for (int i = 0; i < total; i++) { for (int j = i + 1; j < total; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } return a[0]; } public static void main(String args[]) { int x[]={44,66,99,77,33,11,55}; System.out.println("The smallest number: "+getSmallest(x,6)); } } |
The smallest number:11
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: Java program, Java programs, Java programs with examples, Java programs with output, java tutorial, Java tutorials, java tutorials with examples