Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Largest Among 3 in C++

Find the largest of three numbers in C++ with an if / else if / else chain and the && operator. Shows why >= matters when two inputs tie for the maximum.

BeginnerConditional ProgramsExample 2 of 4
largest-among-3.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int a, b, c, largest;
6
7 cout << "Enter three numbers: ";
8 cin >> a >> b >> c;
9
10 if (a >= b && a >= c) {
11 largest = a;
12 } else if (b >= a && b >= c) {
13 largest = b;
14 } else {
15 largest = c;
16 }
17
18 cout << "Largest number is: " << largest << endl;
19
20 return 0;
21}

Sample Output

Largest number is: 25

b won both comparisons in the else if.

In this one

  • How to combine two comparisons with &&
  • How an else if chain stops at the first true test
  • Why >= handles ties and > does not
  • That std::max({a, b, c}) does the same job

How it works

  1. 1

    Read a, b, c in one line: 10 25 15.

  2. 2

    Test a >= b && a >= c: 10 >= 25 is false.

  3. 3

    Test b >= a && b >= c: both true, so largest = 25.

  4. 4

    The final else is skipped.

  5. 5

    Print "Largest number is: 25".

Edge cases

Two equal largest values

Enter 10 10 5.

With >=, the first test passes and largest is 10. If the code used >, both tests would fail and the else would wrongly pick 5.

All three equal

Enter 4 4 4.

The first test passes and 4 is printed. Any variable would be correct here.

What's going on

This C++ program finds the largest of three numbers with if / else if. Ties need >=, or two equal maxima fall through to the wrong variable.

cin >> a >> b >> c;

One line can fill all three. Type 10 25 15 and you get the sample output: 25.

if (a >= b && a >= c)

a is a maximum only if it beats (or ties) both others. For 10, 25, 15 this is false.

else if (b >= a && b >= c)

25 >= 10 and 25 >= 15. True. largest = 25.

else

Whatever is left must be c. No extra comparison needed if the first two failed.

Why >= not >: input 10 10 5. With >, the first test is false (10 is not greater than 10), the second is also false, and you print 5. That's the quiet bug.

std::max({a, b, c}) exists in <algorithm>. The if chain is the version you can write when that isn't allowed.

If two values tie for largest, this program reports that value. It does not say which variable it came from.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

For inputs 8 8 3, what would a version using > print?

Pick an answer to check it.

Question 2

In a && b, when is b skipped?

Pick an answer to check it.

Questions people ask

How do I find the smallest of three?

Flip the comparisons to <=, or use std::min({a, b, c}).

What about more than three numbers?

Store them in an array and loop, keeping the biggest value seen so far. See Array Max & Min.

Does && check both sides every time?

No. If the left side is false, C++ skips the right side. This is called short-circuit evaluation.