Find Biggest of Three Numbers in C

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Conditionals

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 max2 helper (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 (like the reference) plus a max2 composition.

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 >, >=.

  • #include <stdio.h>, int main(void), printf, and integer literals.
  • 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.

The reference program uses >= on both sides so ties are classified into the first matching branch without falling through incorrectly.

If-ladder explicit branches
Nested max max(a,b) then vs c
General N loop + running max

Formal note

Binary max is associative in the usual sense 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.

Example

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 with your eyes from left to right, remembering the tallest seen so far. For exactly three fixed slots, you can hard-code the same comparisons.

14, 7, 22 Max 22
Check
22 ≥ 14 and 22 ≥ 7
Verdict
Matches the sample program output.
5, 5, 3 Max 5
Tie
Two values share the maximum.
Verdict
>= branches return 5 (first branch wins here).
9, 9, 9 Max 9
Triple tie
Every value is largest.
Verdict
First branch returns 9.

Takeaway: the problem 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).

Whole numbers fit the C samples; the preview still runs if you type decimals.

Live result
Press “Run” to see the biggest value.

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.

📜 Pseudocode

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

If–else if ladder (reference style)

Matches the classic interview solution: three mutually exclusive branches with && and >=.

c
#include <stdio.h>

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

int main(void) {
    int number1 = 14;
    int number2 = 7;
    int number3 = 22;
    int result = findBiggest(number1, number2, number3);

    printf("The biggest number is: %d\n", result);
    return 0;
}

Explanation

Each branch names one candidate and proves it beats (or ties) the other two.

num1 >= num2 && num1 >= num3

Candidate 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.

2

Nested two-argument maximum

Same semantics, compact form. Easy to generalize mentally to “fold” over longer arrays in a loop.

c
#include <stdio.h>

static int max2(int a, int b) {
    return (a > b) ? a : b;
}

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

int main(void) {
    int number1 = 14;
    int number2 = 7;
    int number3 = 22;
    int result = findBiggest3(number1, number2, number3);

    printf("The biggest number is: %d\n", result);
    return 0;
}

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, consider fmax from <math.h> if your toolchain allows it and you need NaN rules from the standard.

Interview: write the if-ladder first for clarity; offer max2 nesting as a follow-up refactor.

❓ FAQ

Compare pairs with if-else, or compute max(a,b) once and then max of that result with c. Both return the largest value; using >= instead of > keeps the behavior correct when two or all three numbers are equal.
If you only used strict >, two equal largest values could fall through incorrectly depending on branch order. Chaining >= against both other operands picks a winner whenever the current candidate is tied for largest.
C89/C99 do not provide max for integers in the standard library (math.h has fmax for doubles). For integers, write a small helper or a careful macro; avoid macro side effects in interviews unless asked.
Extend the same idea: either loop with a running maximum, or fold nested max calls. Loops scale better for large N.
A fixed number of comparisons for three values is O(1) time and O(1) extra space.
Comparing ints does not add them, so ordinary max-of-three does not overflow. Beware if you later compute sums or products as part of a larger problem.

🔄 Input / output examples

Both sample programs print one line. Swap literals or use scanf("%d %d %d", &a, &b, &c); for interactive runs.

Inputs (a, b, c)Printed line
14, 7, 22The biggest number is: 22
5, 5, 3The biggest number is: 5
-1, -4, -2The 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.

Ties

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 the smallest index among ties, say so and adjust.

Negatives

All values below zero

Comparisons still work: the “biggest” is the least negative (closest to zero).

Macros

#define MAX(a,b)

Can double-evaluate arguments. Prefer static inline functions or careful macros if you use them at all.

⏱️ Time and space complexity

ApproachTimeExtra space
If-ladder or nested max2 for three intsO(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 &&, or max2(max2(a,b),c).
  • Watch-outs: tie semantics with strict > only, and scaling to N with a loop.
Did you know?

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.

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