Java – Find Largest of Three Numbers

In this tutorial, we shall learn how to find the largest of three numbers using different approaches.

You can find largest of three numbers using if-else statement, if-else-if ladder or ternary operator. We shall go through each of these with example programs.

Example 1 – Find Largest of Three Numbers using If-Else

In this example, we shall use the following algorithm to find the largest of three numbers. We shall realize this algorithm using Java If-Else statement.

Algorithm

  1. Start.
  2. Take three numbers in a, b, c.
  3. Check if a is greater than b.
  4. If above condition is true, go to step 5, else go to step 7.
  5. Check if a is greater than c.
  6. If above condition is true, a is the largest, else c is the largest. Go to step 9.
  7. Check if b is greater than c.
  8. If above condition is true, b is the largest, else c is the largest.
  9. Stop.

Java Program

/**
 * Java Program - Find Largest of Three Numbers
 */

public class LargestInThree {

	public static void main(String[] args) {
		//three numbers
		int a = 20;
		int b = 10;
		int c = 30;
		
		int largest;
		
		//find the largest
		if(a>b) {
			if(a>c) {
				largest = a;
			}
		} else {
			if(b>c) {
				largest = b;
			} else {
				largest = c;
			}
		}
		
		System.out.println(largest + " is the largest.");
	}
}

Ouput

Run the above program and we shall get the largest of three numbers as shown in the following console output.

30 is the largest.
ADVERTISEMENT

Example 2 – Find Largest of Three Numbers using If-Else-If Ladder

In this example, we shall use the following algorithm to find the largest of three numbers. We shall implement the following algorithm using Java if-else-if statement.

Algorithm

  1. Start.
  2. Take three numbers in a, b, c.
  3. Check if a is greater than b and a is greater than c.
  4. If above condition is true, a is largest and go to step 7, else go to step 5.
  5. Check if b is greater than c.
  6. If above condition is true, b is the largest, else c is the largest.
  7. Stop.

Java Program

/**
 * Java Program - Find Largest of Three Numbers
 */

public class LargestInThree {

	public static void main(String[] args) {
		//three numbers
		int a = 20;
		int b = 10;
		int c = 30;
		
		int largest;
		
		//find the largest
		if(a>b && a>c) {
			largest = a;
		} else if (b>c) {
			largest = b;
		} else {
			largest = c;
		}
		
		System.out.print(largest + " is the largest.");
	}
}

Output

Runt he above program and we shall get that 30 is the largest of the taken three numbers.

30 is the largest.

Example – Find Largest of Three Numbers using Ternary Operator

The algorithm for this example, is kind of same as that of the first example, where we used if-else statement. But, in this example, we use nested ternary operator to crunch all the code to a single line.

Java Program

/**
 * Java Program - Find Largest of Three Numbers
 */

public class LargestInThree {

	public static void main(String[] args) {
		//three numbers
		int a = 20;
		int b = 10;
		int c = 30;
		
		int largest = (a>b)?(a>c?a:c):(b>c?b:c);
		
		System.out.print(largest + " is the largest.");
	}
}

Output

Run the above program and you shall get the following output. You may change the values for a, b and c and check the result.

30 is the largest.

Conclusion

In this Java Tutorial, we learned how to find the largest of three given numbers, using different conditional expressions in Java programming language.