In this java program, you’ll learn how to find the largest among the three numbers in java. We’ll find the largest among three numbers using if..else
and nested if..else
statements in Java.
//Java Program to Find the Largest Among Three Numbers Using If...Else Statement
public class JavaPrograms {
public static void main(String[] args) {
double n1 = -6, n2 = 4.9, n3 = 4;
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.");
}
}
}
4.9 is the largest number.
//Java Program to Find the Largest Number Among Three Using Nested if..else Statement
public class JavaPrograms {
public static void main(String[] args) {
double n1 = -6, n2 = 4.9, n3 = 4;
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.");
}
}
}
4.9 is the largest number.
Comments