Java Program to Find the Largest Among Three Numbers

You are currently viewing Java Program to Find the Largest Among Three Numbers

Hey reader! In this tutorial, You are going to learn the concept of the “Java Program to Find the Largest Among Three Numbers”.

Here, You can find the biggest number of three given input numbers in the java program using the if…else and the nested…if conditions. Okay, Let’s start the tutorial to do a task.

Java Program to Find the Largest Among Three Numbers

We can execute a task using the if…else condition below:

public class Largest {

    public static void main(String[] args) {

        double n1 = 26, n2 = 58, n3 = 75;

        if( n1 >= n2 && n1 >= n3)
            System.out.println(n1 + " is the largest number.");

        else if (n2 >= n1 && n2 >= n3)
            System.out.println(n2 + " is the largest number.");

        else
            System.out.println(n3 + " is the largest number.");
    }
}

Execution

Initially, We have to declare the three respective variables to store the three input given numbers. After executing this program, The if…condition will check whether the 1st number is greater than the 2nd and the 3rd number respectively.

If it is true then it will print “1st number is the largest number”. Otherwise, The else…if condition will check whether the 2nd number is greater than the 1st and the 3rd number.

If it is true then it will print “2nd number is the largest number”. Otherwise, The else condition will print “3rd number is the largest number” using the system.out.printIn() function.

Output

Below, You will see the reference output of the Java Program to Find the Largest Among Three Numbers.

75.0 is the largest number.
Read Also : Java Program to Print Hello World

Note

You can also Find largest among three numbers using Java Script and Find largest among three numbers in PHP. Please read those articles to click on the link.

Java Program to Find the Largest Among Three Numbers

The nested…if condition will use to execute a task in the below program.

public class Largest {

    public static void main(String[] args) {

        double n1 = 45, n2 = 36, n3 = 67;

        if(n1 >= n2) {
            if(n1 >= n3)
                System.out.println(n1 + " is the largest number.");
            else
                System.out.println(n3 + " is the largest number.");
        } else {
            if(n2 >= n3)
                System.out.println(n2 + " is the largest number.");
            else
                System.out.println(n3 + " is the largest number.");
        }
    }
}

Explanation

As usual, We need to declare the three input numbers with the respective variables. Later, The nested…if condition will find the biggest number among the three given input numbers respectively. Finally, The system.out.printIn() function will print the result on the output screen.

Output

The example output of the Java Program to Find the Largest Among Three Numbers will be shown below for your reference.

67.0 is the largest number.

Final Words

I hope this section Java Program to Find the Largest of Three Numbers will useful to you. Share your powerful feedback about this article with us. You have any doubts about this topic then ask us via the comment section. Thank You!

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply