HomeJavaJava Program to Find the Largest Number Among Three Numbers

Java Program to Find the Largest Number Among Three Numbers

In this Java tutorial, we will learn how to find the largest number among three numbers using Java

How to Find the Largest Number Among Three Numbers? 

Example 1: using if..else statement 

RUN CODE SNIPPET
public class Main { 

    public static void main(String[] args) { 

        double n1 = -5, n2 = 15.55, n3 = 50; 

        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."); 

    } 

}

OUTPUT 

50.0 is the largest number.

In the above example the numbers –5, 15.55, 50 are stored in the variables n1, n2, n3. 

Here using the if-else statement we find the largest number among the three numbers. 

Example 2: using nested if-else statement 

RUN CODE SNIPPET
public class Main { 
    public static void main(String[] args) { 
      double a = 0, b = 5.5, c = 99.9; 
      if(a >= b) { 
          if(a >= c) 
               System.out.println(a + " is the largest number."); 
            else 
                System.out.println(c + " is the largest number."); 
        } else { 
            if(b >= c) 
                System.out.println(b + " is the largest number."); 
            else 
                System.out.println(c + " is the largest number."); 
        } 
    } 
}

OUTPUT 

99.9 is the largest number.

In the above example the numbers 0, 5.5, 99.9 are stored in the variables a, b, c. 

If a is greater or equal to b and if a is greater than or equal to c, a is the greatest number else, c is the greatest number. 

Else, if b is greater than or equal to both c and a, b is the greatest number else, c is the greatest number. 

Share:

Leave A Reply

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

You May Also Like

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022