Java Program to Find the Largest of Three Numbers

Introduction

Finding the largest of three numbers is a basic programming task often used to practice conditional statements and logical comparisons. This guide will show you how to create a Java program that takes three numbers as input and determines the largest among them.

Problem Statement

Create a Java program that:

  • Takes three integer inputs from the user.
  • Compares the three numbers.
  • Returns and displays the largest number.

Example 1:

  • Input: 3, 5, 7
  • Output: The largest number is 7

Example 2:

  • Input: 10, 20, 15
  • Output: The largest number is 20

Example 3:

  • Input: 25, 25, 25
  • Output: The largest number is 25 (all are equal)

Solution Steps

  1. Prompt for Input: Use the Scanner class to read three integer inputs from the user.
  2. Compare the Numbers Using Conditional Statements: Use if-else or ternary operator to compare the three numbers and determine the largest.
  3. Display the Result: Print the largest number.

Java Program

Approach 1: Using if-else Statements

import java.util.Scanner;

/**
 * Java Program to Find the Largest of Three Numbers using if-else statements
 * Author: https://www.javaguides.net/
 */
public class LargestOfThree {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Prompt the user for input
        System.out.print("Enter the first number: ");
        int num1 = scanner.nextInt();

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

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

        // Step 2: Compare the numbers using if-else statements
        int largest;

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

        // Step 3: Display the result
        System.out.println("The largest number is " + largest);
    }
}

Explanation

  • Input: The program prompts the user to enter three numbers.

  • Comparison:

    • The if-else statements compare the three numbers to determine which one is the largest.
    • If num1 is greater than or equal to both num2 and num3, then num1 is the largest.
    • Otherwise, if num2 is greater than or equal to both num1 and num3, then num2 is the largest.
    • If neither num1 nor num2 is the largest, then num3 is the largest.
  • Output: The program prints the largest number.

Output Example

Example 1:

Enter the first number: 3
Enter the second number: 5
Enter the third number: 7
The largest number is 7

Example 2:

Enter the first number: 10
Enter the second number: 20
Enter the third number: 15
The largest number is 20

Example 3:

Enter the first number: 25
Enter the second number: 25
Enter the third number: 25
The largest number is 25

Approach 2: Using Ternary Operator

import java.util.Scanner;

/**
 * Java Program to Find the Largest of Three Numbers using Ternary Operator
 * Author: https://www.javaguides.net/
 */
public class LargestOfThreeTernary {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Prompt the user for input
        System.out.print("Enter the first number: ");
        int num1 = scanner.nextInt();

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

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

        // Step 2: Compare the numbers using the ternary operator
        int largest = (num1 >= num2) ? (num1 >= num3 ? num1 : num3) : (num2 >= num3 ? num2 : num3);

        // Step 3: Display the result
        System.out.println("The largest number is " + largest);
    }
}

Explanation

  • Comparison Using Ternary Operator:
    • The ternary operator simplifies the comparison by nesting conditional checks:
      • If num1 is greater than or equal to num2, the program checks whether num1 is also greater than or equal to num3.
      • If not, it compares num2 with num3 to find the largest.

Output Example

The output for the ternary operator approach will be the same as the if-else approach.

Conclusion

This Java program demonstrates how to find the largest of three numbers using both if-else statements and the ternary operator. Both approaches are effective and straightforward, with the ternary operator offering a more concise syntax. This basic problem is a good exercise for practicing conditional statements and can be extended to more complex comparisons if needed.

Comments