Java Program to Find the Maximum and Minimum in an Array

Introduction

Finding the maximum and minimum values in an array is a common task in programming. This task involves iterating through the array to identify the smallest and largest elements. This guide will walk you through writing a Java program that finds the maximum and minimum values in a given array.

Problem Statement

Create a Java program that:

  • Prompts the user to enter the size of an array and its elements.
  • Finds the maximum and minimum values in the array.
  • Displays the maximum and minimum values.

Example:

  • Input: [3, 5, 7, 2, 8, -1, 4, 10, 12]
  • Output: "Maximum: 12, Minimum: -1"

Solution Steps

  1. Read the Array Size and Elements: Use the Scanner class to take the size and elements of the array as input from the user.
  2. Initialize Maximum and Minimum Variables: Initialize variables to store the maximum and minimum values.
  3. Traverse the Array: Use a loop to iterate through the array and update the maximum and minimum variables accordingly.
  4. Display the Results: Print the maximum and minimum values.

Java Program

// Java Program to Find the Maximum and Minimum in an Array // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class MaxMinInArray { public static void main(String[] args) { // Step 1: Read the size and elements of the array from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int size = scanner.nextInt(); int[] array = new int[size]; System.out.println("Enter the elements of the array:"); for (int i = 0; i < size; i++) { array[i] = scanner.nextInt(); } // Step 2: Initialize max and min variables int max = array[0]; int min = array[0]; // Step 3: Traverse the array to find max and min for (int i = 1; i < size; i++) { if (array[i] > max) { max = array[i]; } if (array[i] < min) { min = array[i]; } } // Step 4: Display the maximum and minimum values System.out.println("Maximum: " + max); System.out.println("Minimum: " + min); } } 

Explanation

Step 1: Read the Array Size and Elements

  • The Scanner class is used to read the size of the array and its elements. The nextInt() method captures the size and each element.

Step 2: Initialize Maximum and Minimum Variables

  • The first element of the array is assigned to both max and min variables. This serves as the initial reference point for comparison.

Step 3: Traverse the Array

  • A for loop starts from the second element of the array and traverses through the entire array.
  • In each iteration:
    • If the current element is greater than max, update max with the current element.
    • If the current element is smaller than min, update min with the current element.

Step 4: Display the Results

  • The program prints the maximum and minimum values using System.out.println().

Output Example

Example 1:

Enter the size of the array: 9 Enter the elements of the array: 3 5 7 2 8 -1 4 10 12 Maximum: 12 Minimum: -1 

Example 2:

Enter the size of the array: 5 Enter the elements of the array: 23 45 67 89 12 Maximum: 89 Minimum: 12 

Example 3:

Enter the size of the array: 6 Enter the elements of the array: 10 20 5 15 25 30 Maximum: 30 Minimum: 5 

Conclusion

This Java program demonstrates how to find the maximum and minimum values in an array. It covers essential concepts such as array traversal, conditional logic, and basic arithmetic operations, making it a valuable exercise for beginners learning Java programming.

Leave a Comment

Scroll to Top