Java program to find the largest and smallest of n user input numbers

Java program to find the largest and smallest of n user input numbers:

Let’s learn how to write a Java program that finds the largest and smallest of n user-input numbers. We will learn how to solve it by using an ArrayList and without using an ArrayList.

Example 1: Java program by using an ArrayList:

In this example, we will use an ArrayList to hold the user input values. It will ask the user to enter the numbers one by one and insert these into an ArrayList. It will then iterate over the ArrayList to find the maximum and minimum values.

import java.util.ArrayList;
import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        ArrayList<Integer> numList = new ArrayList<>();
        int size, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the size of the list: ");
        size = scanner.nextInt();

        for (int i = 0; i < size; i++) {
            System.out.println("Enter the number for index " + i + ": ");
            numList.add(scanner.nextInt());
        }

        for (int i : numList) {
            if (i > max) max = i;
            if (i < min) min = i;
        }

        System.out.println("Maximum value found: " + max);
        System.out.println("Minimum value found: " + min);
    }
}

Here,

  • numList is an ArrayList to hold the user input numbers.
  • The integer variable size is used to hold the size of the list or total numbers to add to the list, max is used to store the maximum value and min is used to store the minimum value. These values are initialized as the minimum value and maximum value that an integer can hold.
  • The Scanner object is used to read the user inputs.
  • It reads the size of the list and assigns it to the size variable.
  • The first for loop is used to read the numbers. It reads the number and adds it to the list.
  • The second for loop is used to find the maximum and minimum values in the list. It iterates over the items and compares each item with the current max and min values. If the current item is larger than the max variable, it assigns this to the max variable. Similarly, it updates the min variable.
  • The last two lines are used to print the maximum and minimum values.

Let’s see how it works:

Enter the size of the list: 
5
Enter the number for index 0: 
6
Enter the number for index 1: 
3
Enter the number for index 2: 
4
Enter the number for index 3: 
1
Enter the number for index 4: 
9
Maximum value found: 9
Minimum value found: 1

Java find largest smallest n numbers

Example 2: Java program without using an ArrayList:

The above program can be written without using an extra ArrayList. Instead of appending the values to an ArrayList, after each value is entered by the user, it will compare it with the max and min values. So, we will have the maximum and minimum values once all numbers are entered.

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int size, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE, currentValue;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the size of the list: ");
        size = scanner.nextInt();

        for (int i = 0; i < size; i++) {
            System.out.println("Enter the number for index " + i + ": ");
            currentValue = scanner.nextInt();

            if (currentValue > max) max = currentValue;
            if (currentValue < min) min = currentValue;
        }

        System.out.println("Maximum value found: " + max);
        System.out.println("Minimum value found: " + min);
    }
}

So, the program is comparing the user-entered number with max and min on each step. It will give similar output.

Enter the size of the list: 
4
Enter the number for index 0: 
5
Enter the number for index 1: 
9
Enter the number for index 2: 
1
Enter the number for index 3: 
7
Maximum value found: 9
Minimum value found: 1

You might also like: