In this tutorial, Java program to find minimum value in an array.
Here is simple algorithm to find minimum value in the array.
- Initialize sml with arr[0] i.e. first element in the array.
- If current element is less than sml, then set sml to current element.
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 30 31 32 33 34 |
import java.util.Scanner; public class Finder { public static void main(String args[]) { int sml, size, i; int numArr[] = new int[50]; Scanner scan = new Scanner(System.in); System.out.print("Enter array Size : "); size = scan.nextInt(); System.out.print("Enter array elements : "); for(i=0; i<size; i++){ numArr[i] = scan.nextInt(); } System.out.print("Searching for the Smallest Element....\n\n"); sml = numArr[0]; for(i=0; i<size; i++){ if(sml > numArr[i]){ sml = numArr[i]; } } System.out.print("Smallest Element = " + sml); } } |
Output:
Enter array Size : 3
Enter array elements : 45 76 35
Searching for the Smallest Element….Smallest Element = 35
Enter array elements : 45 76 35
Searching for the Smallest Element….Smallest Element = 35
That’s all about Java program to find minimum value in an array
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.