Largest Among 3
Program to find the largest of three numbers
C++ Largest Among 3 Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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:
---
2. Header File: #include <iostream>
#include <iostream>
This header file provides:
cout → for displaying output on the screencin → for reading input from the userWithout this header, we cannot perform input/output operations in C++.
---
3. using namespace std;
For example:
cout, cinstd::cout, std::cinIt 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 userlargest → 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
cin >> a >> b >> c;
a, b, and cExample:
If the user enters:
10 25 15
Then:
a = 10b = 25c = 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`
a is greater than or equal to bAND
greater than or equal to c
a is the largest&& operator means "both conditions must be true"Second condition: `b >= a && b >= c`
b is greater than or equal to aAND
greater than or equal to c
b is the largestElse case:
a nor b is the largest, then c must be the largestelse 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
false
Step 2:
Check if b >= a && b >= c
25 >= 10 →true
25 >= 15 →true
largest = b = 25Result:
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:
10, 10, 5
>= correctly identifies that both 10s are the largest> might miss this case---
9. Displaying the Result
This line:
largestendl moves to the next lineOutput:
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
&&) operator checks multiple conditions simultaneously>= handles cases where numbers might be equalThis 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.