Largest Among 3

Program to find the largest of three numbers

BeginnerTopic: Conditional Programs
Back

C++ Largest Among 3 Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int a, b, c, largest;
    
    cout << "Enter three numbers: ";
    cin >> a >> b >> c;
    
    if (a >= b && a >= c) {
        largest = a;
    } else if (b >= a && b >= c) {
        largest = b;
    } else {
        largest = c;
    }
    
    cout << "Largest number is: " << largest << endl;
    
    return 0;
}
Output
Enter three numbers: 10 25 15
Largest number is: 25

Understanding Largest Among 3

This program teaches how to find the largest number among three given numbers using conditional statements. This is a fundamental problem-solving skill that helps students understand comparison logic, nested conditions, and logical operators in C++.

---

1. What This Program Does

The program takes three numbers as input from the user and determines which one is the largest. It demonstrates:

How to compare multiple values
Using if-else statements for decision making
Logical AND (&&) operator for multiple conditions
Nested conditional structures

---

2. Header File: #include <iostream>

#include <iostream>

This header file provides:

cout → for displaying output on the screen
cin → for reading input from the user

Without this header, we cannot perform input/output operations in C++.

---

3. using namespace std;

This line allows us to use standard C++ functions and objects without writing `std: :` prefix.

For example:

With it → we can write: cout, cin
Without it → we must write: std::cout, std::cin

It makes the code cleaner and easier to read for beginners.

---

4. Declaring Variables

int a, b, c, largest;

The program declares four integer variables:

a, b, c → store the three numbers entered by the user
largest → stores the result (the largest number)

We use int because we're working with whole numbers. If you need to handle decimal numbers, you would use float or double.

---

5. Taking Input From User

`cout << "Enter three numbers: ";`

cin >> a >> b >> c;

The first line displays a message asking the user to enter three numbers
The second line reads three numbers and stores them in variables a, b, and c

Example:

If the user enters:

10 25 15

Then:

a = 10
b = 25
c = 15

---

6. Finding the Largest Number - The Logic

The program uses nested if-else statements to compare the three numbers:

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

largest = a;

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

largest = b;

} else {

largest = c;

}

Understanding the Conditions:

First condition: `a >= b && a >= c`

Checks if a is greater than or equal to b

AND

greater than or equal to c

If both conditions are true, then a is the largest
The && operator means "both conditions must be true"

Second condition: `b >= a && b >= c`

Checks if b is greater than or equal to a

AND

greater than or equal to c

If both are true, then b is the largest

Else case:

If neither a nor b is the largest, then c must be the largest
This is why we use else without a condition

---

7. Step-by-Step Example

Let's trace through with input:

a = 10, b = 25, c = 15

Step 1:

Check if a >= b && a >= c

10 >= 25

false

Since first part is false, the entire condition is

false

Move to next condition

Step 2:

Check if b >= a && b >= c

25 >= 10

true

25 >= 15

true

Both are true, so largest = b = 25

Result:

Largest number is

25

---

8. Why Use >= Instead of >?

We use >= (greater than or equal) instead of > (greater than) to handle cases where two or more numbers are equal.

Example:

If input is:

10, 10, 5

Using >= correctly identifies that both 10s are the largest
Using > might miss this case

---

9. Displaying the Result

`cout << "Largest number is: " << largest << endl;`

This line:

Prints the text "Largest number is: "
Prints the value stored in largest
endl moves to the next line

Output:

Largest number is: 25

---

10. return 0;

This statement indicates that the program executed successfully. In C++, returning 0 from main() means the program completed without errors.

---

Summary

The program compares three numbers using nested if-else statements
Logical AND (&&) operator checks multiple conditions simultaneously
Using >= handles cases where numbers might be equal
This is a fundamental pattern for finding maximum/minimum values
Understanding this logic helps in solving more complex comparison problems

This program is essential for beginners as it teaches conditional logic, which is used in almost every real-world program. Once you master this, you can easily extend it to find the largest among more numbers or find the smallest number.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Largest Among 3

This C++ program is part of the "Conditional Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents