Java Find Min Max Number Array

Java sample code that will determine both the minimum (min) and maximum (max) values based on a list of numbers contained within a Java Array. The for loop is the main processing piece keeps assigning the biggest and smallest value looking at each number in the array.

Java Find Min Max Number Array Source Code


public class JavaFindMinMaxArray 

	public static void main(String[] args) 
		//Define an array of integers
		int numbers[] = new int[] {21,43,12,87,45,67,36,99,54,22,76,19};
		//Assign minimum to the first element
		int minimum = numbers[0];
		//Assign the maximum to the first element
		int maximum = numbers[0];
		//Loop through to find the minimum and maximum
		for(int x=1; x < numbers.length; x++)
			if(numbers[x] > maximum) { maximum = numbers[x]; }			
			else if (numbers[x] < minimum) { minimum = numbers[x]; }		
		//Print out the maximum and minimum values
		System.out.println("Maximum Number: " + maximum);
		System.out.println("Minimum Number: " + minimum);



Java Find Min Max Number Array Output

Maximum Number: 99
Minimum Number: 12

Add a Comment

Your email address will not be published. Required fields are marked *