Java program to find the largest and smallest of three numbers

Java program to find the largest and smallest of three numbers:

In this post, we will learn how to find the largest and smallest of three numbers in Java. We will learn different ways to write this program in this post.

Method 1: By using if..else-if..else conditions:

This method will use two if..else-if..else conditions. One will find the largest number and the another will find the smallest number.

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int first, second, third;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the three numbers: ");
        first = scanner.nextInt();
        second = scanner.nextInt();
        third = scanner.nextInt();

        if (first >= second && first >= third) {
            System.out.println(first + " is the largest number.");
        } else if (second >= first && second >= third) {
            System.out.println(second + " is the largest number.");
        } else {
            System.out.println(third + " is the largest number.");
        }

        if (first <= second && first <= third) {
            System.out.println(first + " is the smallest number.");
        } else if (second <= first && second <= third) {
            System.out.println(second + " is the smallest number.");
        } else {
            System.out.println(third + " is the smallest number.");
        }
    }
}

Here,

  • We declared three integer variables, first, second and third.
  • The Scanner variable scanner is used to read the user input values. It reads the user input numbers and assigns these to these three variables.
  • The first if..else-if..else block finds the largest of the three numbers. It compares each number with the other two numbers to find the largest. Once it finds the largest number, it prints its value.
  • The second if..else-if..else block is finding the smallest number by comparing each number with the other two numbers.

If you run the above program, it will print output as below:

Enter the three numbers: 
5
6
1
6 is the largest number.
1 is the smallest number.

Method 2: By using separate variables:

With this method, we will use two separate variables to hold the largest and smallest numbers. These variables will be compared with the other two variables to find the largest and the smallest variables among the given three numbers.

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int first, second, third;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the three numbers: ");
        first = scanner.nextInt();
        second = scanner.nextInt();
        third = scanner.nextInt();

        int smallest = first;
        int largest = first;

        if (second < smallest) {
            smallest = second;
        }
        if (third < smallest) {
            smallest = third;
        }

        if (second > largest) {
            largest = second;
        }
        if (third > largest) {
            largest = third;
        }

        System.out.println(smallest + " is the smallest number.");
        System.out.println(largest + " is the smallest number.");
    }
}

This is almost similar to the above program.

  • We assigned the first number to the smallest and largest variables.
  • These variables are compared with the second and third numbers to find the smallest and largest values.

It will print similar results.

Enter the three numbers: 
4
1
2
1 is the smallest number.
4 is the smallest number.

You can also remove the braces from the if statements and concisely write the code.

....
....
if (second < smallest) smallest = second;
if (third < smallest) smallest = third;

if (second > largest) largest = second;
if (third > largest) largest = third;
....
....

Method 3: By using a separate list:

With this method, we will use a list of size two. The first number will be assigned to the largest and the smallest variables. The second and third numbers will be assigned to the first and the second element of the list.

We can compare the largest and the smallest variables with the list elements to find the largest and smallest numbers among the given three numbers.

import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        int[] nums = new int[2];
        int smallest, largest;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the three numbers: ");
        smallest = scanner.nextInt();
        largest = smallest;

        nums[0] = scanner.nextInt();
        nums[1] = scanner.nextInt();

        for (int num : nums) {
            if (num < smallest) smallest = num;
            if (num > largest) largest = num;
        }

        System.out.println(smallest + " is the smallest number.");
        System.out.println(largest + " is the smallest number.");
    }
}

Here,

  • nums is an array of size 2.
  • The first number is assigned to the smallest and largest variables.
  • The second and third numbers are added to the first and the second indices of the array.
  • The for loop iterates over the array numbers and compares the smallest and largest variables with each of the numbers in the array. It updates these variables on each step if any smaller or larger value is found.

It will give similar results.

Java example program largest and smallest of three numbers

You might also like: