Write a Java program to find the largest of three numbers.

Finding the largest of three numbers is a common exercise in learning programming languages, and it is an excellent way to understand the basic concepts of conditional statements and comparison operators. In this article, we will explore how to write a Java program to find the largest of three numbers, with detailed explanations of each part of the code.

Understanding the Problem

Before diving into the code, let’s break down the problem. We need to:

  1. Take three numbers as input from the user.
  2. Compare these numbers to determine which one is the largest.
  3. Print the largest number.

Setting Up the Environment

Before writing the Java program, ensure that you have the Java Development Kit (JDK) installed on your computer. You can download it from the official Oracle website. You will also need a text editor or an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or NetBeans.

Writing the Java Program

Now, let’s write a Java program to find the largest of three numbers. We will use the Scanner class to read input from the user and if-else statements to compare the numbers.

Here is the complete code:

import java.util.Scanner;

public class LargestOfThree {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter three numbers
        System.out.print("Enter the first number: ");
        double num1 = scanner.nextDouble();
        
        System.out.print("Enter the second number: ");
        double num2 = scanner.nextDouble();
        
        System.out.print("Enter the third number: ");
        double num3 = scanner.nextDouble();

        // Determine the largest number
        double largest;
        
        if (num1 >= num2 && num1 >= num3) {
            largest = num1;
        } else if (num2 >= num1 && num2 >= num3) {
            largest = num2;
        } else {
            largest = num3;
        }

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

Explanation of the Code

Let’s break down the program step by step to understand how it works.

1. Importing the Scanner Class

We import the Scanner class from the java.util package to read input from the user.

import java.util.Scanner;

2. Creating the Main Class

We create a public class named LargestOfThree. In Java, the class name should match the filename.

public class LargestOfThree {

3. Main Method

The main method is the entry point of the program. Inside this method, we will write the code to find the largest of three numbers.

public static void main(String[] args) {

4. Creating a Scanner Object

We create a Scanner object to read input from the user.

Scanner scanner = new Scanner(System.in);

5. Reading Input from the User

We prompt the user to enter three numbers and store these values in variables. Here, we use double to accommodate both integer and floating-point numbers.

System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();

System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();

System.out.print("Enter the third number: ");
double num3 = scanner.nextDouble();

6. Determining the Largest Number

We use if-else statements to compare the three numbers and determine which one is the largest. The logic is straightforward:

  • First, we check if num1 is greater than or equal to both num2 and num3. If true, num1 is the largest.
  • If the first condition is false, we then check if num2 is greater than or equal to both num1 and num3. If true, num2 is the largest.
  • If both conditions above are false, num3 is the largest.
double largest;

if (num1 >= num2 && num1 >= num3) {
    largest = num1;
} else if (num2 >= num1 && num2 >= num3) {
    largest = num2;
} else {
    largest = num3;
}

7. Printing the Largest Number

Finally, we print the largest number.

System.out.println("The largest number is: " + largest);

Running the Program

To run the program, follow these steps:

  1. Save the code in a file named LargestOfThree.java.
  2. Open a command prompt or terminal and navigate to the directory where you saved the file.
  3. Compile the program using the following command:
javac LargestOfThree.java

4. Run the compiled program using the following command:

java LargestOfThree
  1. Follow the prompts to enter three numbers. The program will display the largest number.

What did we learn from this article?

Writing a Java program to find the largest of three numbers is a great exercise for understanding the basics of conditional statements and user input handling. This simple yet fundamental problem helps in building a strong foundation for more complex programming tasks. By understanding how to compare numbers and use conditional statements, you can solve a wide range of problems in Java. Keep practicing and exploring more problems to enhance your programming skills. Happy coding!

Leave a Comment