Last Updated on May 18, 2023 by Prepbytes

A number is an arithmetical value used in calculations and counting that represents a certain quantity. It is communicated by words, symbols, or figures. For centuries, people have used these numbers.

They aid with pattern recognition and algorithm building and offer lots of entertaining features. They become one of the most fun things to experiment with in interviews and other types of questions as a result.

## How to Find the Greatest of Three Numbers in Java?

We will majorly discuss three approaches to find the greatest number from the three in Java. We will use if-else statements, and ternary operators to implement all our logic. Let’s discuss the flowchart and algorithm first because the logic for all three approaches is the same.

### Flowchart for the Largest of 3 Numbers

### Algorithm to Find the Largest of Three Numbers

- Start
- Add values to A, B, and C for comparison.
- Check whether A is Greater than B.
- If it is true, see if A is greater than C.
- If it is true, print ‘A’ as the greatest number.
- ‘C’ will be printed as the greatest number if false.

- If that is false, check if B is greater than C.
- If it is true, print ‘B’ as the highest number.
- ‘C’ will be printed as the greatest number if false.

- If it is true, see if A is greater than C.
- End

### Approaches to Find the Greatest of Three Numbers in Java

- Using Ternary Operators
- Using If Else
- Using Collections.max() method and ArrayList Java

**Approach 1: Using the Ternary operator**

The conditional operator’s syntax is as follows: ans = (conditional expression)? execute if true, execute if false

- Run the statement before the colon if the condition is true.
- If the condition is false, then execute the sentence following the colon.

largest = z > (x>y ? x:y) ? z:((x>y) ? x:y);

**Example:**

```
x = 5, y= 10, z = 3
largest = 3>(5>10 ? 5:10) ? 3: ((5>10) ? 5:10);
largest = 3>10 ? 3 : 10
largest = 10
```

**Code Implementation**

import java.io.*; class PrepBytes { // Function to find the biggest of three numbers static int biggestOfThree(int x, int y, int z) { return z > (x > y ? x : y) ? z : ((x > y) ? x : y); } // Main driver function public static void main(String[] args) { // Declaring variables for 3 numbers int a, b, c; // Variable holding the largest number int largest; a = 5; b = 10; c = 3; // Calling the above function in main largest = biggestOfThree(a, b, c); // Printing the largest number System.out.println(largest + " is the largest number."); } }

**Output**

`10 is the largest number.`

**Approach 2: Using the If-else Statements**

The if else statement in this approach compares numbers and checks for the largest number. If will determine whether or not ‘x’ is greater than ‘y’ and ‘z’. If ‘else’ is used, it will determine whether or not ‘y’ is greater than ‘x’ and ‘z’. And ‘z’ will be the largest number if none of the two conditions is true.

**Code Implementation**

import java.io.*; class PrepBytes { // Function to find the biggest of three numbers static int biggestOfThree(int x, int y, int z) { // Comparing all 3 numbers if (x >= y && x >= z) // Returning 1st number if largest return x; // Comparing 2nd no with 1st and 3rd no else if (y >= x && y >= z) // Return z if the above conditions are false return y; else // Returning 3rd no, Its sure it is greatest return z; } // Main driver function public static void main(String[] args) { int a, b, c, largest; // Considering random integers three numbers a = 5; b = 10; c = 3; // Calling the function in main() body largest = biggestOfThree(a, b, c); // Printing the largest number System.out.println(largest + " is the largest number."); } }

**Output**

`10 is the largest number.`

**Approach 3: Using Collections.max() Method and ArrayList**

In this approach, we will add all the numbers in an ArrayList, then we will use the Collections.max() method to find the largest number in the array.

**Code Implementation**

// Java Program to Find the Largest of three Numbers // Using Collections.max() method import java.lang.*; import java.util.*; class PrepBytes { public static void main(String[] args) { int a, b, c; // Considering random integers three numbers a = 5; b = 10; c = 3; ArrayList<Integer> x = new ArrayList<>(); x.add(a); x.add(b); x.add(c); // Printing the largest number System.out.println(Collections.max(x) + " is the largest number."); } }

**Output**

`10 is the largest number.`

**Time Complexity:** O(n)

**Auxiliary Space:** O(n), where n is the length of ArrayList.

**Conclusion**

In conclusion, finding the largest number from three numbers in Java can be accomplished using different approaches, each with its own advantages and disadvantages.

## FAQ related to Greatest of Three Numbers in Java

**Q1. What is the purpose of finding the largest number from three numbers in Java?**

**Ans.** The purpose of finding the largest number from three numbers in Java is to determine the maximum value among three given input values.

**Q2. What are the different approaches discussed in the article for finding the largest number from three numbers in Java?**

**Ans.** The article discusses three approaches for finding the largest number from three numbers in Java ternary operator approach, if-else approach, and ArrayList and Collections.max method approach.

**Q3. Which approach is the most efficient for finding the largest number from three numbers in Java?**

**Ans.** The ArrayList and Collections.max method approach is the most efficient for finding the largest number from three numbers in Java, especially when dealing with larger datasets.

**Q4. What are the advantages of the ternary operator approach for finding the largest number from three numbers in Java?**

**Ans.** The ternary operator approach for finding the largest number from three numbers in Java is concise, easy to read, and can be useful for simple conditions.

**Q5. What are the advantages of the if-else approach for finding the largest number from three numbers in Java?**

**Ans.** The if-else approach for finding the largest number from three numbers in Java is more flexible and can handle more complex conditions.

**Q6. What is the role of the ArrayList and Collections.max method in finding the largest number from three numbers in Java?**

**Ans.** The ArrayList and Collections.max method approach involves storing the three input values in an ArrayList and then using the Collections.max method to find the largest number.