Find Biggest of Three Numbers in Java

Beginner
⏱️ 9 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Conditionals

What You’ll Learn

Finding the biggest of three numbers is a classic conditional warm-up. This tutorial covers the comparison rule, a live preview, algorithm steps, worked Java examples, edge cases, and complexity.

Goal

Largest of a, b, c

Return the value that is not smaller than the other two.

If-Else Ladder

Explicit logic

Compare with >= so ties still pick a valid maximum.

Built-in max

Math.max(Math.max(a, b), c)

Short and clear for real code; still explain the ladder in interviews.

Ties & Negatives

Edge cases

Equal largest values and all-negative inputs still work the same way.

Live Preview

Try a, b, c

Enter three numbers and see the maximum instantly in the browser.

O(1)

Complexity

A fixed number of comparisons — constant time and constant extra space.

Introduction

Given three numbers a, b, and c, the task is to find the biggest value — the one that is greater than or equal to both of the others.

You can write an explicit if-else ladder or nest Math.max calls. If two or three values tie for largest, returning any one of those tied values is valid.

Why it matters?

It trains clear branching, tie handling, and the habit of explaining O(1) complexity for fixed-size inputs.

Key Highlights

Prefer >=

Handles equal largest values without special cases.

Negatives OK

Among negatives, the largest is the least negative.

Two Styles

Write the ladder for interviews; use Math.max in apps.

Constant Cost

Three fixed inputs need only a few comparisons.

In short: compare a, b, and c; return the value that is greater than or equal to the other two.

📝 Problem & Approach

Given three numbers a, b, and c, return the maximum among them.

java
// Example: a=14, b=7, c=22
// 22 >= 14 and 22 >= 7  → biggest is 22

Inputs & Outputs

ItemTypeDescription
a, b, cint / floatThree numbers to compare (ints or doubles both work).
Return / printsame typeThe largest value among the three (any tied max is fine).

Minimal workflow

Pseudocode
function findBiggest(a, b, c):
    if a >= b && a >= c:
        return a
    if b >= a && b >= c:
        return b
    return c

Method comparison

MethodIdeaNotes
If-else ladderExplicit comparisons with >=Best for showing interview logic
Math.maxMath.max(Math.max(a, b), c)Shortest production style

⚡ Quick Reference

GoalPattern
a is biggesta >= b && a >= c
b is biggestb >= a && b >= c
Otherwisereturn c
One-linerMath.max(Math.max(a, b), c)
Nested maxMath.max(a, Math.max(b, c))
Many valuesa running-max loop or a running-max loop

📋 If-Else vs Math.max vs Nested max2

Same answer — different clarity and interview signaling.

If-else
compare >=

Shows branching clearly; preferred whiteboard style

Math.max(Math.max(a, b), c)
built-in

Idiomatic Java for real applications

Nested max
Math.max(a, Math.max(b, c))

Same idea; useful when teaching pairwise max

Interview tip
ladder first

Write if-else, then mention Math.max as a shortcut

Context

When This Problem Shows Up

Reach for biggest-of-three drills when branching and comparisons matter.

  1. Interview warm-ups

    Quick check of if-else structure and tie handling.

  2. Teaching conditionals

    First multi-branch program after simple if/else.

  3. Gateway to N-max

    Natural step toward a running-max loop over a list.

  4. Small fixed comparisons

    UI scores, three sensors, or three candidate prices.

  5. Not for huge lists alone

    For many values, use a loop or a running-max loop instead of nested ifs.

Key benefit: one tiny problem that covers branching, ties, negatives, and O(1) complexity talk.

🔮 Live Preview

Enter three numbers and check the biggest value instantly.

Integers and decimals both work.

Live result
Press "Run" to see the biggest value.

Examples Gallery

Three complete Java programs — if-else ladder, Math.max, and custom max2. Click View Output to reveal sample console results.

📚 Getting Started

Explicit comparisons — the interview default.

Example 1 — If-Else Approach

Check a, then b, otherwise return c — using >= for ties.

java
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);
    }
}

How It Works

The first branch wins when num1 is a maximum. The second branch covers when num2 is a maximum. Otherwise num3 must be biggest.

⚡ Idiomatic Java

Same answer with the standard library helper.

Example 2 — Nested Math.max

Math.max takes two args, so nest a call for three values.

java
public class Main {
    static int findBiggestWithMax(int a, int b, int c) {
        return Math.max(Math.max(a, b), c);
    }

    public static void main(String[] args) {
        int a = 14;
        int b = 7;
        int c = 22;
        System.out.println("The biggest number is: " + findBiggestWithMax(a, b, c));
    }
}

How It Works

Math.max compares two arguments and returns the larger. Nesting covers three values. Great for production code after you have already explained the comparison logic.

🔁 Pairwise Style

Build the three-way max from two-way max calls.

Example 3 — Custom max2 Helper

First take the larger of b and c, then compare with a.

java
public class Main {
    static int max2(int a, int b) {
        return (a >= b) ? a : b;
    }

    static int findBiggestNested(int a, int b, int c) {
        return max2(a, max2(b, c));
    }

    public static void main(String[] args) {
        System.out.println(findBiggestNested(5, 5, 3));
        System.out.println(findBiggestNested(-1, -4, -2));
    }
}

How It Works

max2(b, c) reduces two values to one candidate; then max2(a, …) finishes the job. The tie case 5,5,3 returns 5; the all-negative case returns -1.

🧠 How the Algorithm Decides

1

Test a

If a >= b and a >= c, a is a maximum — return it.

Branch
2

Test b

Otherwise, if b >= a and b >= c, return b.

Branch
3

Else c

If the first two checks fail, c must be the biggest.

Fallback
=

Maximum found

Return that value — ties are already handled by >=.

🔎 Worked Walkthrough — 14, 7, 22

Trace the if-else ladder with a = 14, b = 7, c = 22.

CheckConditionResult
a biggest?14 >= 7 && 14 >= 22false (14 < 22)
b biggest?7 >= 14 && 7 >= 22false
Fallbackreturn c22

Tie example: for 5, 5, 3, the first branch 5 >= 5 && 5 >= 3 is true, so the answer is 5.

Use Cases

Where biggest-of-three checks show up beyond the interview prompt.

1. Interview Warm-Ups

Tests if-else structure and clear return values.

Example: write findBiggest(a, b, c).

2. Teaching Branches

Makes multi-way decisions feel concrete.

Example: chalkboard walkthrough of 14, 7, 22.

3. Small Scoreboards

Pick the best of three fixed candidates.

Example: three quiz attempts.

4. Gateway to N-Max

Leads into running-max loops over lists.

Example: max of an array.

5. Complexity Practice

Argue O(1) time for a fixed three inputs.

Example: “how many comparisons?”

6. Tie Talk

Shows why >= is safer than strict >.

Example: inputs 5, 5, 3.

Pro Tip: keep a pure findBiggest helper and print outside it — easier to test ties and negatives.

Advantages

Why this pattern works well in interviews and classwork.

  1. 1. Tiny and Clear

    A few comparisons map directly to the definition of maximum.

  2. 2. Constant Cost

    Fixed three inputs mean O(1) time and O(1) extra space.

  3. 3. Easy Built-in Shortcut

    Math.max(Math.max(a, b), c) keeps application code short after you know the logic.

  4. 4. Natural Edge-Case Story

    Ties and negatives give interviewers clear follow-up questions.

Pro Tip: say “return any tied maximum” before coding — it justifies >= immediately.

Usage Tips

Small habits that keep biggest-of-three code interview-ready.

  1. 1. Use >= for Ties

    Strict > can skip equal largest values depending on branch order.

  2. 2. Lead with the Ladder

    Write if-else first in interviews, then mention Math.max.

  3. 3. Spot-Check Negatives

    Assert Math.max(Math.max(-1, -4), -2) == -1 before calling it done.

  4. 4. Keep the Helper Pure

    Return the value; print outside the function.

  5. 5. Scale Carefully

    For many numbers, switch to a loop or a running-max loop.

Pro Tip: dry-run both a clear winner (14, 7, 22) and a tie (5, 5, 3) on paper once.

Common Pitfalls

Mistakes that commonly break biggest-of-three solutions.

  1. 1. Using Strict > Carelessly

    Tie cases may fall through branches unexpectedly depending on order.

    → Prefer >= when any tied maximum is acceptable.

  2. 2. Assuming Negatives Are Invalid

    Maximum among negatives is still well-defined.

    → Test an all-negative triple.

  3. 3. Comparing Strings by Accident

    Input read as text compares lexicographically, not numerically.

    → Convert with int / float before comparing.

  4. 4. Only Calling Built-in max in Interviews

    Interviewers often want to see the comparison logic.

    → Write the ladder, then mention Math.max as a shortcut.

  5. 5. Exploding Ifs for N Values

    Nested conditions do not scale past three inputs.

    → Use a running maximum when N grows.

Edge Cases

Check these inputs before calling the solution done.

Ties

Equal largest values

For 5, 5, 3, the answer is still 5.

All equal

Any value works

For 2, 2, 2, return 2.

Negatives

All values negative

The greatest value is the least negative number.

Zeros

Mix with negatives

0, -1, -5 → 0.

Floats

Same idea

Comparisons work for doubles; adjust types if needed.

Input type

Parse before compare

Do not compare string digits as text.

⚖️ Facts Worth Knowing

Handy follow-ups interviewers sometimes ask.

  • Associative. Math.max(a, Math.max(b, c)) equals Math.max(Math.max(a, b), c).
  • Idempotent. Math.max(x, x) == x, which is why ties are easy.
  • Dual problem. Biggest of three mirrors smallest of three with min / <=.
  • Fixed size. Complexity stays O(1) only while the input count is fixed at three.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Verify classics

  • 14, 7, 22 → 22
  • 5, 5, 3 → 5
  • -1, -4, -2 → -1

2. Write smallest of three

  • Mirror the ladder with <=
  • Or use min(a, b, c)

3. Extend to a list

  • Running max over N values
  • State O(N) time

4. Implement both styles

  • If-else and max
  • Assert they agree on a test set

Notes

  • Definition. Biggest means greater than or equal to each of the other two.
  • Use >= so equal largest values still return correctly.
  • In interviews, show the ladder first; mention Math.max(Math.max(a, b), c) second.
  • State O(1) time and O(1) extra space for exactly three inputs.

Quick Takeaway: compare a, b, and c with a few >= checks (or Math.max) and return the largest.

⏱️ Time and Space Complexity

ProgramTimeExtra space
If-else checks for 3 numbersO(1)O(1)
Nested Math.maxO(1)O(1)
Nested Math.max(a, Math.max(b, c))O(1)O(1)
Wrap Up

🎉 Conclusion

Finding the biggest of three numbers is a clean branching exercise: compare with >=, handle ties, and return the winner. Master the if-else ladder first, then use Math.max(Math.max(a, b), c) when brevity matters.

Practice the three examples above, then continue to binary-to-decimal for a classic base-conversion warm-up.

Prefer >= for ties, test negatives, and state O(1) time for exactly three inputs.

💡 Best Practices

✅ Do

  • Compare with >= for clear ties
  • Show the if-else ladder in interviews
  • Test ties and all-negative inputs
  • Mention Math.max(Math.max(a, b), c) as a shortcut
  • State O(1) time and space

❌ Don’t

  • Rely only on max when logic must be shown
  • Compare unparsed strings as numbers
  • Assume negatives are invalid
  • Nest ifs endlessly for long lists
  • Forget that any tied maximum is valid

Key Takeaways

Knowledge Unlocked

Five things to remember about biggest of three

Pick the maximum the interview-friendly way.

5
Core concepts
if 02

Ladder

if / else if / else

Code
M 03

Math.max

Nested shortcut

Code
= 04

Ties

Any max is OK

Edge
O 05

Complexity

O(1) time

Analysis

❓ Frequently Asked Questions

You compare the values using if-else conditions, or nest Math.max calls. Both return the largest value.
Using >= handles ties clearly. If two numbers are equal and largest, one valid maximum is still returned correctly.
Yes. Java's Math.max takes two arguments, so nest it: Math.max(Math.max(a, b), c). In interviews, writing the if-else version first helps show your logic.
For exactly three numbers, time complexity is O(1) and extra space is O(1).
Yes. Comparisons work the same way for negative values too.
Use a loop with a running maximum, or nest Math.max calls across the array.
Any of them is a valid answer — they all share the same maximum value.
Yes. It is equivalent and still O(1) for three fixed arguments.

Did you Know? 🔊

To find the biggest of 3 values, you only need a few comparisons, so the solution runs in O(1) time and O(1) space.

Continue to Binary to Decimal

Learn how to convert a binary number into its decimal equivalent in Java.

Binary to decimal tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful