Given three values a, b, and c, the biggest is the one that is greater than or equal to both of the others.
Remember
Example: a = 14, b = 7, c = 22
22 >= 14 and 22 >= 7 → biggest is 22
Ties: a = 10, b = 10, c = 3
either 10 is a valid maximum
a
b
c
Biggest
14
7
22
22
10
10
3
10
-5
-2
-9
-2
5
5
5
5
Approach
How to Find the Biggest of Three Numbers in Java
Compare pairwise (or nest max) and prefer >= so ties stay correct.
If a >= b and a >= c, return a.
Else if b >= a and b >= c, return b.
Otherwise return c.
Shortcut: Math.max(Math.max(a, b), c).
Method
Idea
Time
Best for
If-else ladder
Explicit >= checks
O(1)
Interviews, default
Nested Math.max
Math.max(Math.max(a, b), c)
O(1)
Short production code
Custom max2
Pairwise helper, then nest
O(1)
Teaching pairwise max
Pseudocode
Pseudocode
function Biggest(a, b, c):
if a >= b and a >= c:
return a
if b >= a and b >= c:
return b
return c
Reference
Cheat Sheet
Keep these patterns handy while coding.
Topic
Detail
a wins
a >= b && a >= c
b wins
b >= a && b >= c
else
return c
One-liner
Math.max(Math.max(a, b), c)
Many values
Use a running-max loop (see Maximum of Array)
Try it
Live Preview
Enter three numbers to see which is biggest.
Integers and decimals both work. Tap a chip or type your own values.
Result
a = 14, b = 7, c = 22
biggest = 22
Trace
Worked Walkthrough — 14, 7, 22
Apply the if-else ladder step by step.
Calculation
a = 14, b = 7, c = 22
Is a >= b and a >= c?
14 >= 7 → true
14 >= 22 → false
→ a is not the biggest
Is b >= a and b >= c?
7 >= 14 → false
→ b is not the biggest
Otherwise return c
biggest = 22
Code
Java Programs
Start with an if-else ladder, then nest Math.max, and finish with a small max2 helper.
Example 1 — If-Else Ladder
Explicit comparisons with >= — the interview default.
Java
import java.util.Scanner;
class BiggestIfElse {
static int findBiggest(int a, int b, int c) {
if (a >= b && a >= c) {
return a;
} else if (b >= a && b >= c) {
return b;
}
return c;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println("Biggest = " + findBiggest(a, b, c));
sc.close();
}
}
Output
Enter three numbers: 14 7 22
Biggest = 22
How It Works
Read three integers with Scanner.
If a is at least as large as b and c, return a.
Else if b wins both comparisons, return b.
Otherwise c must be the maximum.
Sample 14, 7, 22 prints 22.
Example 2 — Nested Math.max
Short and idiomatic — still explain the ladder in interviews.
Java
class BiggestMathMax {
static int findBiggest(int a, int b, int c) {
return Math.max(Math.max(a, b), c);
}
public static void main(String[] args) {
int a = 14, b = 7, c = 22;
System.out.println("Biggest = " + findBiggest(a, b, c));
}
}
Output
Biggest = 22
How It Works
Math.max(a, b) picks the larger of the first pair.
Nest that result with c: Math.max(…, c).
Same answer as the if-else ladder — fewer lines.
Math.max only takes two args, so nesting is required.
14 vs 7 → 14; 14 vs 22 → 22.
Example 3 — Custom max2 Helper
Write your own pairwise max, then nest it — great for teaching.
Java
class BiggestMax2 {
static int max2(int x, int y) {
return (x >= y) ? x : y;
}
static int findBiggest(int a, int b, int c) {
return max2(max2(a, b), c);
}
public static void main(String[] args) {
System.out.println(findBiggest(-5, -2, -9));
}
}
Output
-2
How It Works
max2 returns the larger of two values with >=.
max2(a, b) first, then compare that result with c.
Negatives work: −5 vs −2 → −2; −2 vs −9 → −2.
Same idea as nesting Math.max.
Prints -2 for the sample.
Edge Cases & Pitfalls
Check these before calling the solution done.
Ties
Prefer >= over >
Equal largest values still return a valid maximum with >=.
Negatives
Largest can still be negative
Among −5, −2, −9 the biggest is −2 — do not assume a positive answer.
N values
Do not nest forever
For many numbers, use a running-max loop instead of nested ifs.
Types
Match the numeric type
Use double (and Math.max overloads) when you need decimals.
Analysis
Time and Space Complexity
Program
Time
Extra space
If-else / Math.max / max2
O(1)
O(1)
Running max over N values
O(N)
O(1)
Three fixed inputs need only a few comparisons — constant time and space.
Remember
Key Takeaways
Rule: return the value that is >= both others.
Ties: use >= so equal maxima still work.
Shortcut:Math.max(Math.max(a, b), c).
Test: 14, 7, 22 → 22; −5, −2, −9 → −2.
One line: compare three values; return the one that is not smaller than the other two.
Frequently Asked Questions
Compare with if-else using >=, or nest Math.max: Math.max(Math.max(a, b), c). Both return the largest value.
>= handles ties. If two values share the maximum, one valid answer is still returned.
Yes. Math.max takes two arguments, so nest it. In interviews, write the if-else version first to show the logic.
For exactly three numbers, time is O(1) and extra space is O(1).
Yes. Comparisons treat negatives the same way — among −5, −2, −9 the biggest is −2.