This is an example code snippet to find the minimum and maximum number in a given Array using Java programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Scanner; public class FindMinMaxInArray { public static void main(String[] args) { int nums[]=new int[]{3, 5, 6, 2, 4, 15, 120}; int max = 0, min= 0; max = min = nums[0]; for (int i = 1; i < nums.length; i++) { if(nums[i]>max) max = nums[i]; if(nums[i]<min) min = nums[i]; } System.out.println("Minimum number is "+min + "\nMaximum number is "+max); } } |
Output:
Minimum number is 2Maximum number is 120
No comments:
Post a Comment