Java Program to Find Largest of Three Numbers

In this post, we will learn to code the Java Program to Find Largest of Three Numbers. Let’s understand How to Find the Largest Among Three Numbers in Java Programming Language and about the conditional if-else statement in Java.

Let’s see the Java Program to Find Largest of Three Numbers. We are going to see two approaches to find the largest of the three numbers.

Java Program to Find Largest of Three Numbers

We are going to code the Java Program to Find Largest of Three Numbers using nested if-else.

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the 1st Number : ");
        int num1 = sc.nextInt();
        
        System.out.println("Enter the 2nd Number : ");
        int num2 = sc.nextInt();
        
        System.out.println("Enter the 3rd Number : ");
        int num3 = sc.nextInt();
        
        if(num1 > num2){
            if(num1 > num3){
                System.out.println("Largest among the three numbers is "+num1);
            }else{
                System.out.println("Largest among the three numbers is "+num3);
            }
        }else{
            if(num2 > num3){
              System.out.println("Largest among the three numbers is "+num2);
            }else{
                System.out.println("Largest among the three numbers is "+num3);
            }
        }
        
    }
    
}

Output

Enter the 1st Number : 123

Enter the 2nd Number : 345

Enter the 3rd Number : 234

Largest among the three numbers is 345

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the 1st Number : ");
        int num1 = sc.nextInt();
        
        System.out.println("Enter the 2nd Number : ");
        int num2 = sc.nextInt();
        
        System.out.println("Enter the 3rd Number : ");
        int num3 = sc.nextInt();

In this program, we are taking the input of three numbers using the Scanner Class to take input in Java.

        if(num1 > num2){
            if(num1 > num3){
                System.out.println("Largest among the three numbers is "+num1);
            }else{
                System.out.println("Largest among the three numbers is "+num3);
            }
        }else{
            if(num2 > num3){
              System.out.println("Largest among the three numbers is "+num2);
            }else{
                System.out.println("Largest among the three numbers is "+num3);
            }
        }

Then, using the nested if-else ladder we find the largest of three numbers in Java.

If the first number is greater than the second number, then we check if the first number is greater than the third number and then we print the first number as the largest number otherwise we print the third number.

And if the first number is not greater than the second number, then we check if the second number is greater than the third number or not. If the second number is greater than the third number then we print the second number as the largest number else we print the third number.

This is the Java Program to Find Largest of Three Numbers Using Nested if-else.

Now, let’s see the Java Program to Find Largest of Three Numbers Using else-if Ladder.

Java Program to Find Largest of Three Numbers Using else-if Ladder

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the 1st Number : ");
        int num1 = sc.nextInt();
        
        System.out.println("Enter the 2nd Number : ");
        int num2 = sc.nextInt();
        
        System.out.println("Enter the 3rd Number : ");
        int num3 = sc.nextInt();
        
        if( (num1 > num2) && (num1 > num3) ){
            
            System.out.println("The Largest Number Among the Three is "+num1);
            
        }else if((num2 > num1) && (num2 > num3)){
            
            System.out.println("The Largest Number Among the Three is "+num2);
            
        }else{
            
            System.out.println("The Largest Number Among the Three is "+num3);
            
        }
    }
    
}

Output


Enter the 1st Number : 5123

Enter the 2nd Number : 345

Enter the 3rd Number : 2344

The Largest Number Among the Three is 5123

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the 1st Number : ");
        int num1 = sc.nextInt();
        
        System.out.println("Enter the 2nd Number : ");
        int num2 = sc.nextInt();
        
        System.out.println("Enter the 3rd Number : ");
        int num3 = sc.nextInt();

In this program, we are taking the input of three numbers using the Scanner Class to take input in Java.

        
        if( (num1 > num2) && (num1 > num3) ){
            
            System.out.println("The Largest Number Among the Three is "+num1);
            
        }else if((num2 > num1) && (num2 > num3)){
            
            System.out.println("The Largest Number Among the Three is "+num2);
            
        }else{
            
            System.out.println("The Largest Number Among the Three is "+num3);
            
        }

Then, we use the else-if ladder to check if the first number is greater than both the second number and the third number then, we print the first number otherwise if the second number is greater than both the first number and third number then, we print the second number otherwise we print the third number.

This is the Java Program to Find Largest of Three Numbers Using else-if Ladder.

Conclusion

I hope after going through this post, you understand Java Program to Find Largest of Three Numbers. We discussed two approaches in this post.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *