0
0
CppProgramBeginner · 2 min read

C++ Program to Find Largest of Three Numbers

To find the largest of three numbers in C++, use if-else statements to compare the numbers and print the largest, for example: if (a >= b && a >= c) cout << a; else if b >= a && b >= c then cout << b; else cout << c;.
📋

Examples

Inputa=3, b=7, c=5
Output7
Inputa=10, b=10, c=2
Output10
Inputa=-1, b=-5, c=-3
Output-1
🧠

How to Think About It

To find the largest number among three, compare the first number with the other two using && (and) operator. If it is greater or equal to both, it is the largest. Otherwise, check the second number similarly. If neither the first nor second is largest, the third must be the largest.
📐

Algorithm

1
Get input values for three numbers a, b, and c
2
Check if a is greater than or equal to both b and c
3
If yes, a is the largest; print a
4
Else check if b is greater than or equal to both a and c
5
If yes, b is the largest; print b
6
Otherwise, print c as the largest
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if (a >= b && a >= c)
        cout << a << endl;
    else if (b >= a && b >= c)
        cout << b << endl;
    else
        cout << c << endl;
    return 0;
}
Output
7
🔍

Dry Run

Let's trace input a=3, b=7, c=5 through the code

1

Input values

a=3, b=7, c=5

2

Check if a >= b and a >= c

3 >= 7 is false, so condition fails

3

Check if b >= a and b >= c

7 >= 3 is true and 7 >= 5 is true, so condition passes

4

Print b

Output 7

StepConditionResult
Check a3 >= 7 && 3 >= 5false
Check b7 >= 3 && 7 >= 5true
Printb7
💡

Why This Works

Step 1: Compare first number

We use if (a >= b && a >= c) to check if the first number is greater than or equal to both others.

Step 2: Compare second number

If the first check fails, we check b >= a && b >= c to see if the second number is largest.

Step 3: Default to third number

If neither first nor second is largest, the third number must be largest, so we print c.

🔄

Alternative Approaches

Using std::max function
cpp
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    int largest = max(a, max(b, c));
    cout << largest << endl;
    return 0;
}
This approach uses the built-in <code>max</code> function for cleaner code but requires including <code>algorithm</code>.
Using ternary operator
cpp
#include <iostream>
using namespace std;

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    int largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
    cout << largest << endl;
    return 0;
}
This uses nested ternary operators for a compact one-line comparison but can be harder to read for beginners.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of comparisons regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store inputs and results, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; using std::max is clean and efficient, while if-else is straightforward and easy to understand.

ApproachTimeSpaceBest For
If-else statementsO(1)O(1)Beginners learning basic comparisons
std::max functionO(1)O(1)Clean and concise code
Ternary operatorO(1)O(1)Compact code, experienced users
💡
Use std::max for a simple and readable way to find the largest number.
⚠️
Beginners often forget to use && to check both conditions together, causing wrong results.