C++ 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 numbers a, b, and c, the biggest is the one that is greater than or equal to both of the others.

Remember
biggest(a, b, c) = the maximum among a, b, and c

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

Ties: if two share the max, either is a valid answer
abcBiggest
1472222
5535
-1-4-2-1
9999

How to Find the Biggest of Three Numbers in C++

Compare pairwise — a few comparisons are enough for three fixed values.

  1. If a >= b and a >= c, return a.
  2. Else if b >= a and b >= c, return b.
  3. Otherwise return c.
MethodIdeaTimeBest for
If-else ladderExplicit comparisonsO(1)Interviews, clarity
Nested std::maxmax(max(a,b), c)O(1)Idiomatic C++
Custom max2Pairwise helper, then nestO(1)Teaching pairwise max

Pseudocode

Pseudocode
function Biggest(a, b, c):
    if a >= b and a >= c:
        return a
    else if b >= a and b >= c:
        return b
    else:
        return c

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
GoalReturn the maximum of three values
TiesUse >= so equal max values still work
Librarystd::max from <algorithm>
NegativesComparisons work the same way
ComplexityO(1) time and space

Live Preview

Enter three numbers to see which one is biggest and why.

Any integers (including negatives). Tap a chip or type values.

Result
a = 14, b = 7, c = 22
22 >= 14 and 22 >= 7
Biggest = 22

Worked Walkthrough — 14, 7, 22

Test each branch of the if-else ladder.

Calculation
a = 14, b = 7, c = 22

Is a biggest?
  14 >= 7  ✓  but  14 >= 22  ✗

Is b biggest?
  7 >= 14  ✗

Otherwise c is biggest
  Biggest = 22

C++ Programs

Start with an if-else ladder. Then nest std::max, and finish with a custom pairwise helper.

Example 1 — If-Else Approach

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

C++
#include <iostream>

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() {
    int number1 = 14;
    int number2 = 7;
    int number3 = 22;
    int result = findBiggest(number1, number2, number3);
    std::cout << "The biggest number is: " << result << "\n";
    return 0;
}

How It Works

  1. If num1 is >= both others, return it.
  2. Else if num2 is >= both others, return it.
  3. Otherwise num3 must be the biggest.
  4. >= keeps ties correct (equal max values still match).
  5. For 14, 7, 22 the third branch returns 22.

Example 2 — Nested std::max

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

C++
#include <iostream>
#include <algorithm>

int findBiggestWithMax(int a, int b, int c) {
    return std::max(std::max(a, b), c);
}

int main() {
    int a = 14;
    int b = 7;
    int c = 22;
    std::cout << "The biggest number is: " << findBiggestWithMax(a, b, c) << "\n";
    return 0;
}

How It Works

  1. Include <algorithm> for std::max.
  2. std::max(a, b) returns the larger of the first pair.
  3. Nest with c to cover all three values.
  4. Same answer as the if-else version — less code.
  5. Great after you have already explained the comparison logic.

Example 3 — Custom max2 Helper

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

C++
#include <iostream>

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

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

int main() {
    std::cout << findBiggestNested(5, 5, 3) << "\n";
    std::cout << findBiggestNested(-1, -4, -2) << "\n";
    return 0;
}

How It Works

  1. max2 returns the larger of two values (ties keep the first).
  2. max2(b, c) reduces two values to one candidate.
  3. max2(a, …) finishes the three-way max.
  4. Tie case 5, 5, 3 returns 5.
  5. All-negative case -1, -4, -2 returns -1.

Edge Cases & Pitfalls

Check these before calling the solution done.

Ties

Use >= not only >

Equal maximum values still need a correct return. Prefer >=.

Negatives

Largest can be negative

Among -1, -4, -2 the biggest is -1 — not the closest to zero by absolute value.

All equal

Any value is fine

If a = b = c, every branch that uses >= returns the shared maximum.

N values

Extend with a loop

For many numbers, use a running maximum (or max of an array) instead of nested ifs.

Time and Space Complexity

ProgramTimeExtra space
If-else / std::max / max2O(1)O(1)
N numbers (loop)O(N)O(1)

Three fixed comparisons never grow with input size — constant work.

Key Takeaways

  • Goal: return the maximum among three values.
  • Interview: write if-else first; mention std::max after.
  • Ties: prefer >= so equal max values work.
  • Cost: O(1) time and space for three fixed inputs.

One line: compare until one value is greater than or equal to both others.

Frequently Asked Questions

Compare with if-else, or nest std::max calls. Both return the largest value.
Using >= handles ties clearly. If two numbers are equal and largest, one valid maximum is still returned.
Yes. Include <algorithm>. Nest it for three values: std::max(std::max(a, b), c). In interviews, show the if-else version first.
For exactly three numbers, time is O(1) and extra space is O(1).
Yes. Comparisons work the same way for negative values.

Next: Binary to Decimal

Convert a binary value to its decimal (base-10) form.

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