Java Program to Find the Largest of Three Numbers

1. Introduction

Determining the largest number out of three is a fundamental programming exercise often used to introduce conditional statements. This problem helps in understanding how to compare numerical values and make decisions based on those comparisons. In this blog post, we present a simple Java program that takes three numbers as input and finds the largest among them.

2. Program Steps

1. Import the Scanner class to read user input.

2. Declare three variables to store the numbers.

3. Use the Scanner object to read three numbers from the user.

4. Compare the numbers using if-else statements to find the largest one.

5. Display the largest number.

6. Close the Scanner object to prevent resource leaks.

3. Code Program

import java.util.Scanner;

public class LargestOfThreeNumbers {
    public static void main(String[] args) {
        // Creating a Scanner object for reading input
        Scanner scanner = new Scanner(System.in);

        // Prompting user to enter three numbers
        System.out.println("Enter three numbers:");
        double firstNumber = scanner.nextDouble();
        double secondNumber = scanner.nextDouble();
        double thirdNumber = scanner.nextDouble();

        // Closing the scanner
        scanner.close();

        // Finding the largest number
        double largest;
        if (firstNumber > secondNumber && firstNumber > thirdNumber) {
            largest = firstNumber;
        } else if (secondNumber > firstNumber && secondNumber > thirdNumber) {
            largest = secondNumber;
        } else {
            largest = thirdNumber;
        }

        // Displaying the largest number
        System.out.println("The largest number is: " + largest);
    }
}

Output:

Enter three numbers:
45
78
12
The largest number is: 78

Explanation:

1. The program begins by importing the Scanner class, which is used for reading the user's input from the console.

2. Three variables (firstNumber, secondNumber, thirdNumber) are declared to store the numbers entered by the user.

3. The Scanner object reads three numbers from the console and stores them in the respective variables.

4. An if-else statement compares the three numbers. It checks each number against the others to determine which one is the largest.

5. After identifying the largest number, its value is stored in the largest variable.

6. The program prints the largest number to the console with an explanatory message.

7. Finally, the Scanner object is closed to avoid resource leaks, ensuring good resource management practices.

Java Program to Find the Largest of Three Numbers using Math.max() Method

Finding the largest of three numbers is a common programming task that can also be achieved using Java's built-in functions. This method simplifies the code and reduces the need for explicit comparison logic. In this section, we will utilize the Math.max() function, provided by Java, to find the largest number among three inputs efficiently.

1. Program Steps

1. Import the Scanner class to read user input.

2. Declare variables to store the three numbers.

3. Use the Scanner object to capture three numbers from the user.

4. Use the Math.max() function to find the largest number among the three.

5. Display the largest number.

6. Close the Scanner object to prevent resource leaks.

2. Code Program

import java.util.Scanner;

public class LargestOfThreeUsingFunction {
    public static void main(String[] args) {
        // Creating a Scanner object for reading input
        Scanner scanner = new Scanner(System.in);

        // Prompting user to enter three numbers
        System.out.println("Enter three numbers:");
        double firstNumber = scanner.nextDouble();
        double secondNumber = scanner.nextDouble();
        double thirdNumber = scanner.nextDouble();

        // Closing the scanner
        scanner.close();

        // Finding the largest number using Math.max()
        double largest = Math.max(firstNumber, Math.max(secondNumber, thirdNumber));

        // Displaying the largest number
        System.out.println("The largest number is: " + largest);
    }
}

Output:

Enter three numbers:
34
56
12
The largest number is: 56

Explanation:

1. The program begins by importing the Scanner class to read the user's input from the console.

2. Three variables are declared to store the numbers the user enters.

3. The user is prompted to enter three numbers, which are read from the console and stored in the declared variables.

4. To find the largest number, the Math.max() function is employed twice. The function is called once to compare the first two numbers and again to compare the result with the third number. This nested use of Math.max() ensures that the largest of the three numbers is found efficiently.

5. The largest number is then printed to the console.

6. Finally, the Scanner object is closed to avoid potential resource leaks, adhering to good programming practices. This example demonstrates how Java's built-in functions can be used to simplify common programming tasks.

Comments