- Check
22 ≥ 14and22 ≥ 7- Verdict
- Matches the sample program output.
Find Biggest of Three Numbers in Java
What you’ll learn
- How to pick the largest of three integers with a clear if–else if–else ladder.
- An equivalent nested max style using a tiny
max2helper (same result, different shape). - Why
>=matters when values can tie, plus a browser live preview.
Overview
Given three integers, return the one that is not smaller than the other two. When two or three values tie for largest, any of the tied values is a correct “biggest”; the programs on this page return one of them deterministically.
Two programs
Classic if-ladder plus a max2 composition (or nested Math.max).
Live preview
Type three integers and see the maximum immediately.
Interview polish
Mention ties, extension to N values, and O(1) cost for three fixed slots.
Prerequisites
Comfort with if, else if, else, and relational operators >, >=.
public static void main(String[] args), integer variables, and printing withSystem.out.println.- Optional: logical
&&to combine two comparisons in one condition.
What does “biggest of three” mean?
For integers a, b, and c, the maximum (biggest) is any value m in {a,b,c} such that m ≥ a, m ≥ b, and m ≥ c. If the largest value appears more than once, those inequalities still hold.
Using >= makes ties behave predictably: the first matching branch returns a tied winner instead of falling through by accident.
Formal note
Binary max is associative for totally ordered types: max(a, b, c) = max(max(a, b), c). That identity is the correctness argument behind the two-argument helper version (or nested Math.max).
max(14, 7, 22) = 22 because 22 is greater than or equal to both 14 and 7.
Intuition and examples
Picture three poles of different heights: scan from left to right, remembering the tallest seen so far. For exactly three fixed slots, you can hard-code the same comparisons.
- Tie
- Two values share the maximum.
- Verdict
>=branches return 5 (first branch wins).
- Triple tie
- Every value is largest.
- Verdict
- First branch returns 9.
Takeaway: this is total-order comparison, not sorting the whole list.
Live preview
Enter three integers. The widget uses the same ordering as JavaScript Math.max on three numbers (handles ties naturally).
Algorithm
Goal: return the largest of three comparable values a, b, c.
Test whether a wins
If a >= b and a >= c, then a is a maximum; return it.
Else test whether b wins
If b >= a and b >= c, return b.
Otherwise return c
If neither a nor b dominates both others, c must.
Alternative: fold with max2
Let max2(u,v) return the larger of two values. Then max3(a,b,c) = max2(max2(a,b), c). In Java you can also write Math.max(Math.max(a,b), c).
📜 Pseudocode
function findBiggest(a, b, c):
if a >= b and a >= c:
return a
if b >= a and b >= c:
return b
return cIf–else if ladder (reference style)
Classic interview solution: three mutually exclusive branches with && and >=.
public class Main {
static int findBiggest(int num1, int num2, int num3) {
if (num1 >= num2 && num1 >= num3) {
return num1;
} else if (num2 >= num1 && num2 >= num3) {
return num2;
}
return num3;
}
public static void main(String[] args) {
int number1 = 14;
int number2 = 7;
int number3 = 22;
int result = findBiggest(number1, number2, number3);
System.out.println("The biggest number is: " + result);
}
}Explanation
Each branch names one candidate and proves it beats (or ties) the other two.
num1 >= num2 && num1 >= num3Candidate num1. Both comparisons must hold so num1 is not smaller than either rival.
else if (num2 >= num1 && num2 >= num3)Only if the first test failed. Then ask the same style of question for num2.
return num3;Last case. No third if is needed: if num1 and num2 are not maximal, num3 is.
Nested two-argument maximum
Same semantics, compact form. Easy to generalize mentally to a loop for longer lists.
public class Main {
static int max2(int a, int b) {
return (a > b) ? a : b;
}
static int findBiggest3(int a, int b, int c) {
return max2(max2(a, b), c);
// or: return Math.max(Math.max(a, b), c);
}
public static void main(String[] args) {
int number1 = 14;
int number2 = 7;
int number3 = 22;
int result = findBiggest3(number1, number2, number3);
System.out.println("The biggest number is: " + result);
}
}Explanation
max2 uses strict >; ties pick the left operand, which is fine because any tied largest is acceptable.
max2(max2(a, b), c)Associative fold. First reduce the first pair, then compare the winner against c.
Optimization
Three values only. Any correct solution is already O(1); micro-optimizations are rarely asked.
Larger N. Read into an array and track best in one pass instead of hand-unrolling comparisons.
Floating types. For double values, use Math.max but remember NaN rules if you are dealing with invalid data.
Interview: write the if-ladder first for clarity; offer max2 nesting as a follow-up refactor.
❓ FAQ
🔄 Input / output examples
Both sample programs print one line. Swap literals or read interactive inputs using Scanner.
Inputs (a, b, c) | Printed line |
|---|---|
14, 7, 22 | The biggest number is: 22 |
5, 5, 3 | The biggest number is: 5 |
-1, -4, -2 | The biggest number is: -1 |
Edge cases and pitfalls
Watch branch order when you use only strict >, and be explicit about ties if the problem wants a specific tie-break rule.
Two or three equal maxima
The if-ladder with >= returns the first candidate that can tie-win. That is usually acceptable; if the spec needs a specific tie break, mention it and adjust.
All values below zero
Comparisons still work: the “biggest” is the least negative (closest to zero).
Prefer Math.max or a method
In Java, methods have no macro side effects. You can use Math.max or write your own max2 to keep it readable.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| If-ladder or nested max for three ints | O(1) | O(1) |
Max of N numbers (single pass) | O(N) | O(1) |
Both programs on this page use only a few integer locals: auxiliary space is constant.
Summary
- Task: return the maximum of three integers (ties allowed).
- Patterns: if-ladder with
>=and&&, ormax2(max2(a,b),c)/ nestedMath.max. - Watch-outs: tie semantics with strict
>only, and scaling toNwith a loop.
Comparing three values takes at most three pairwise checks in the obvious if-ladder form. The same answer can be built by nesting max(a,b) twice — both styles are O(1) time and O(1) space.
9 people found this page helpful
