Java Biggest of Three Numbers Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is the Biggest of Three Numbers?

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
abcBiggest
1472222
1010310
-5-2-9-2
5555

How to Find the Biggest of Three Numbers in Java

Compare pairwise (or nest max) and prefer >= so ties stay correct.

  1. If a >= b and a >= c, return a.
  2. Else if b >= a and b >= c, return b.
  3. Otherwise return c.
  4. Shortcut: Math.max(Math.max(a, b), c).
MethodIdeaTimeBest for
If-else ladderExplicit >= checksO(1)Interviews, default
Nested Math.maxMath.max(Math.max(a, b), c)O(1)Short production code
Custom max2Pairwise helper, then nestO(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

Cheat Sheet

Keep these patterns handy while coding.

TopicDetail
a winsa >= b && a >= c
b winsb >= a && b >= c
elsereturn c
One-linerMath.max(Math.max(a, b), c)
Many valuesUse a running-max loop (see Maximum of Array)

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

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

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

How It Works

  1. Read three integers with Scanner.
  2. If a is at least as large as b and c, return a.
  3. Else if b wins both comparisons, return b.
  4. Otherwise c must be the maximum.
  5. 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));
    }
}

How It Works

  1. Math.max(a, b) picks the larger of the first pair.
  2. Nest that result with c: Math.max(…, c).
  3. Same answer as the if-else ladder — fewer lines.
  4. Math.max only takes two args, so nesting is required.
  5. 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));
    }
}

How It Works

  1. max2 returns the larger of two values with >=.
  2. max2(a, b) first, then compare that result with c.
  3. Negatives work: −5 vs −2 → −2; −2 vs −9 → −2.
  4. Same idea as nesting Math.max.
  5. 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.

Time and Space Complexity

ProgramTimeExtra space
If-else / Math.max / max2O(1)O(1)
Running max over N valuesO(N)O(1)

Three fixed inputs need only a few comparisons — constant time and space.

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.

Next: Binary to Decimal

Convert a binary string into its decimal integer value.

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.

18 people found this page helpful