Problem Statement: Given three numbers x, y, and z of which aim is to get the largest among these three numbers.

Example:
Input: x = 7, y = 20, z = 56
Output: 56 // value stored in variable z
Flowchart For Largest of 3 numbers:

Algorithm to find the largest of three numbers:
1. Start
2. Read the three numbers to be compared, as A, B and C
3. Check if A is greater than B.
3.1 If true, then check if A is greater than C
If true, print 'A' as the greatest number
If false, print 'C' as the greatest number
3.2 If false, then check if B is greater than C
If true, print 'B' as the greatest number
If false, print 'C' as the greatest number
4. End
Approach 1: Using Ternary operator
The syntax for the conditional operator:
ans = (conditional expression) ? execute if true : execute if false
- If the condition is true then execute the statement before the colon
- If the condition is false then execute a statement after colon so
largest = z > (x>y ? x:y) ? z:((x>y) ? x:y);
Illustration:
x = 5, y= 10, z = 3
largest = 3>(5>10 ? 5:10) ? 3: ((5>10) ? 5:10);
largest = 3>10 ? 3 : 10
largest = 10
import java.io.*;
class GFG {
// Function to find the largest of three numbers using if-else
static int biggestOfThree(int x, int y, int z) {
// Compare all three numbers
if (x >= y && x >= z)
// x is greater than or equal to both y and z
return x;
else if (y >= x && y >= z)
// y is greater than or equal to both x and z
return y;
else
// z is the largest number
return z;
}
public static void main(String[] args) {
// Declaring three numbers
int a = 5;
int b = 10;
int c = 3;
// Calling the function to find the largest number
int 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
This method uses if-else statements to find the largest among three numbers.
- if checks whether x is greater than both y and z, and else if checks whether y is greater than both.
- If both conditions are false, then z is considered the largest number.
- st number.
import java.io.*;
class GFG {
// Function to find the largest of three numbers using if-else
static int biggestOfThree(int x, int y, int z) {
// Compare all three numbers
if (x >= y && x >= z)
// x is greater than or equal to both y and z
return x;
else if (y >= x && y >= z)
// y is greater than or equal to both x and z
return y;
else
// z is the largest number
return z;
}
public static void main(String[] args) {
// Declaring three numbers
int a = 5;
int b = 10;
int c = 3;
// Calling the function to find the largest number
int 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
import java.lang.*;
import java.util.*;
class GFG {
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.
Approach 4: Using Math.max() method
This approach utilizes the Math.max() method from the java.lang.Math class. It takes two numbers as arguments and returns the larger of the two. This method is chained to find the largest between the larger of num1 and num2 and num3.
public class LargestNumber {
public static void main(String[] args) {
int num1 = 10, num2 = 20, num3 = 15;
int largest = Math.max(Math.max(num1, num2), num3);
System.out.println("The largest number is: " + largest);
}
}
Output
The largest number is: 20