Signup/Sign In
PUBLISHED ON: MAY 4, 2021

Java Program to find the Largest of three numbers

In this tutorial, we will learn how to find the largest of the three numbers in java. But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java

Input: Enter the first number: 67

Enter the second number: 89

Enter the third number: 92

Output: The largest number is 92

The above problem can be solved in three ways:

Approach 1: Using If else statement

Approach 2: Using the ternary operator

Approach 3: Using the nested if statement

Let us look at each of these approaches separately.

Program 1: Java Program to find the largest of three numbers

In this program, we will learn how to find the largest of three numbers using if-else statements.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare three variables.
  4. Ask the user to initialize the variables.
  5. Use the if-else statement to find the largest of the three numbers.
  6. Display the result.
  7. Stop

Below is the code for the same.

//Java Program to find the largest of three numbers
import java.util.*;
public class Main
{ 
    // Driver Program 
    public static void main(String[] args) 
    { 
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the first number ");
        int num1 = sc.nextInt(); 
        System.out.println("Enter the second number ");
        int num2 = sc.nextInt(); 
        System.out.println("Enter the third number ");
        int num3 = sc.nextInt(); 
        //Using if else statement find the largest of three numbers
        if( num1 >= num2 && num1 >= num3)
          System.out.println(num1+" is the largest Number");

        else if (num2 >= num1 && num2 >= num3)
          System.out.println(num2+" is the largest Number");

        else
          System.out.println(num3+" is the largest Number");
       
    } 
} 


Enter the first number 1
Enter the second number 8
Enter the third number 6
8 is the largest Number

Program 2: Java Program to find the largest of three numbers

In this program, we will learn how to find the largest of three numbers using a ternary operator.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare three variables.
  4. Ask the user to initialize the variables.
  5. Use the ternary operator to find the largest of the three numbers.
  6. Display the result.
  7. Stop

Below is the code for the same.

//Java Program to find the largest of three numbers
import java.util.*;
public class Main
{ 
    // Driver Program 
    public static void main(String[] args) 
    { 
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the first number ");
        int num1 = sc.nextInt(); 
        System.out.println("Enter the second number ");
        int num2 = sc.nextInt(); 
        System.out.println("Enter the third number ");
        int num3 = sc.nextInt(); 
        //Use the ternary operator to check the largest of the three numbers
        int largest = num3 > (num1 > num2 ? num1 : num2) ? num3 : ((num1 > num2) ? num1 : num2);  
        System.out.println("The largest number is: "+largest); 
    } 
} 


Enter the first number 31
Enter the second number 27
Enter the third number 18
The largest number is: 31

Program 3: Java Program to find the largest of three numbers

In this program, we will learn how to find the largest of three numbers using a nested if statement.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare three variables.
  4. Ask the user to initialize the variables.
  5. Use the nested if statement to find the largest of the three numbers.
  6. Display the result.
  7. Stop

Below is the code for the same.

//Java Program to find the largest of three numbers
import java.util.*;
public class Main
{ 
    // Driver Program 
    public static void main(String[] args) 
    { 
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the first number ");
        int num1 = sc.nextInt(); 
        System.out.println("Enter the second number ");
        int num2 = sc.nextInt(); 
        System.out.println("Enter the third number ");
        int num3 = sc.nextInt(); 
        if(num1 >= num2)   
        {  
            if(num1 >= num3)  
               System.out.println("The largest number is: "+num1);  
            else  
               System.out.println("The largest number is: "+num3);  
        }   
        else   
        {  
            if(num2 >= num3)  
               System.out.println("The largest number is: "+num2);  
            else
               System.out.println("The largest number is: "+num3);  
        }       
    } 
} 


Enter the first number 18
Enter the second number 72
Enter the third number 21
The largest number is: 72



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.