;

Java Program to Find the Largest Among Three Numbers


Tutorialsrack 19/04/2021 Java

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.

Example 1: Java Program to Find the Largest Among Three Numbers Using If...Else Statement

Example 1: Java Program to Find the Largest Among Three Numbers Using If...Else Statement
//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.");
		}
	}
}
Output

4.9 is the largest number.

Example 2: Java Program to Find the Largest Number Among Three Using Nested if...else Statement

Example 2: Java Program to Find the Largest Number Among Three Using Nested if...else Statement
//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.");
		}
	}
}
Output

4.9 is the largest number.


Related Posts



Comments

Recent Posts
Tags