How to Find Max and Min Array Number in Java


This example focuses on how to find the largest and smallest number in an array.

Source Code

package com.beginner.examples;

public class MaxMinExample {

    public static void main(String[] args){
    	
    	int a[]={11,312,147,65,67,17,29,2,40,18};

    	int max = a[0];
    	int min = a[0];

    	for(int i = 0; i< a.length - 1; i++)
    	{
    		//find the largest one
	    	if(max  a[i])
	    	{
	    		min = a[i];
	    	}
    	}
    	
    	System.out.println("The max number is : " + max);
		System.out.println("The min number is : " + min);
    }
}

Output:

The max number is : 312
The min number is : 2
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments