🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

WAP to find the largest of three numbers in Java  


Logical Steps:
  • To find the largest of three numbers, follow these steps:
    1. Take three numbers.
    2. Compare the first number with the second and third numbers using conditional statements.
    3. Use nested if-else statements to determine which number is the largest:
      • If the first number is greater than both the second and third, it is the largest.
      • Else, check if the second number is greater than the other two; if true, it is the largest.
      • Otherwise, the third number is the largest.
    4. Print the largest number as the result.
Program:
  • Below is the simple program:
    public class LargestOfThreeNumbers
    {
        public static void main(String[] args)
        {
            int no1=10, no2=20, no3=30;
    
            // Finding and displaying the largest number
            if (no1>no2 && no1>no3)
            {
                System.out.println("\nThe largest number is : " + no1);
            }
            else if (no2>no1 && no2>no3)
            {
                System.out.println("\nThe largest number is : " + no2);
            }
            else
            {
                System.out.println("\nThe largest number is : " + no3);
            }
        }
    }
    Output:
    The largest number is : 30
  • Below is the program by taking user input:
    import java.util.Scanner;
    
    public class LargestOfThreeNumbers
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
    
            System.out.print("Enter number 1: ");
            int no1 = scanner.nextInt();
    
            System.out.print("Enter number 2: ");
            int no2 = scanner.nextInt();
    
            System.out.print("Enter number 3: ");
            int no3 = scanner.nextInt();
    
            // Finding and displaying the largest number
            if (no1>no2 && no1>no3)
            {
                System.out.println("\nThe largest number is : " + no1);
            }
            else if (no2>no1 && no2>no3)
            {
                System.out.println("\nThe largest number is : " + no2);
            }
            else
            {
                System.out.println("\nThe largest number is : " + no3);
            }
    
            scanner.close();
        }
    }
    Output:
    Enter number 1: 100
    Enter number 2: 200
    Enter number 3: 300
    
    The largest number is : 300
Program Explanation:
  • Import Scanner Class:
    • The Scanner class is imported to take user input.
  • Declare Variables:
    • Three integer variables (no1, no2 and no3) are declared to store the user’s input.
  • Take Input:
    • Use scanner.nextInt() to read integer values from the user.
  • Comparison Logic:
    • The if condition checks if no1 is greater than or equal to both no2 and no3. If true, num1 is the largest.
    • The else if condition checks if no2 is greater than or equal to both no1 and no3. If true, no2 is the largest.
    • If none of the above conditions are true, it means no3 is the largest.
  • Display the Result:
    • The program prints the largest number to the console, informing the user which number is the largest among the three.
  • Close Scanner:
    • The scanner.close() method is used to close the input stream and release resources.