Menu

Showing posts with label Find min and max number from a array. Show all posts
Showing posts with label Find min and max number from a array. Show all posts

Find minimum and maximum number from a given array

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 2
Maximum number is 120

Related articles: