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 SNIPPETpublic 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 SNIPPETpublic 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.Â