How to use if-else Statements In C++
Algorithm
- Start.
- Input a, b, and c.
- Check the condition a>=b
- If step 3 is True go to step 5 else go to step 9.
- Check the condition a>=c.
- If step 5 is True go to step 7 else go to step 8.
- Print “The Largest Among 3 is: a and go to step 13
- Print “The Largest Among 3 is: c and go to step 13.
- Check the condition b>=c.
- If step 9 is True go to step 11 else go to step 12.
- Print “The Largest Among 3 is: b and go to step 13.
- Print “The Largest Among 3 is: c and go to step 13.
- Stop.
C++ Program to Find the Largest Number using if-else.
C++
// C++ Program to Find Largest Among // Three Numbers Using if-else // Statement #include <bits/stdc++.h> using namespace std; // Driver code int main() { int a, b, c; cout << "Enter the three numbers a, b & c" << endl; cin >> a >> b >> c; if (a >= b) { // If 'a' is greater than or equal to 'b', compare // 'a' with 'c' if (a >= c) { // If 'a' is also greater than or equal to 'c', // it is the largest number cout << "The Largest Among Three Numbers is : " << a << endl; } else { // If 'a' is not greater than or equal to 'c', // 'c' must be the largest number cout << "The Largest Among Three Numbers is : " << c << endl; } } else { // If 'b' is greater than 'a', compare 'b' with 'c' if (b >= c) { // If 'b' is also greater than or equal to 'c', // it is the largest number cout << "The Largest Among Three Numbers is : " << b << endl; } else { // If 'b' is not greater than or equal to 'c', // 'c' must be the largest number cout << "The Largest Among Three Numbers is : " << c << endl; } } return 0; } |
Output
Enter the three numbers a, b & c The Largest Among Three Numbers is : 4196384
Complexity Analysis
- Time Complexity: O(1)
- Auxiliary Space: O(1)
C++ Program to Find Largest Among Three Numbers
There are many different methods using which we find the maximum or largest number among three numbers. In this article, we will learn three such methods to find the largest among three numbers in C++.
Example:
Input: a = 1, b = 2, c = 45 Output: The Largest Among 3 is 45
Contact Us