In this tutorial, you will see how to find the largest among three numbers using a C++ program.
Example,
Input: num1= 12, num2 = 108, num3= 100
Output: Largest number = 108
Input: num1= 200, num2 = 180, num3= 10
Output: Largest number = 200
Below mentioned C++ program asks the user to enter three integers numbers, then it compares all three numbers together to find the largest number using the mentioned methods:
- Logical operator and if condition.
- Nested if-else.
- If-else Ladder.
- Ternary conditions.
- Logical operator and ternary conditions.
General logic to find the largest number of three given numbers:
- C++ program asks to enter three integer numbers.
- Now compare all three numbers together using any of the mentioned methods. But here I am considering nested if-else for understanding the concept.
- Compare num1 with num2.
- If num1 is greater than num2, then check if num1 is greater than num3.
- If true, then print ‘num1’ as the greatest number.
- If false, then print ‘num3’ as the greatest number.
- If num2 is greater than num1, then check if num2 is greater than num3.
- If true, then print ‘num2’ as the greatest number.
- If false, then print ‘num3’ as the greatest number.
Flow Chart:
Using the logical operator and if condition:
I have used the logical operator and if statement to find the largest number of three numbers in C++.
#include <iostream> int main() { int num1, num2, num3; std::cout<<" Enter the number1 = "; std::cin >> num1; std::cout<<" Enter the number2 = "; std::cin >> num2; std::cout<<" Enter the number3 = "; std::cin >> num3; if (num1 >= num2 && num1 >= num3) { std::cout<< num1 << "is the largest number."<<std::endl; } if (num2 >= num1 && num2 >= num3) { std::cout<< num2 << "is the largest number."<<std::endl; } if (num3 >= num1 && num3 >= num2) { std::cout<< num3 << " is the largest number."<<std::endl; } return 0; }
Using the nested if-else:
I have used the nested if-else statement to find the largest number of three numbers in C++.
#include <iostream> int main() { int num1, num2, num3; std::cout<<" Enter the number1 = "; std::cin >> num1; std::cout<<" Enter the number2 = "; std::cin >> num2; std::cout<<" Enter the number3 = "; std::cin >> num3; if (num1 >= num2) { if (num1 >= num3) { std::cout<< num1 << "is the largest number."<<std::endl; } else { std::cout<< num3 << "is the largest number."<<std::endl; } } else { if (num2 >= num3) { std::cout<< num2 << " is the largest number."<<std::endl; } else { std::cout<< num3 << "is the largest number."<<std::endl; } } return 0; }
Using the if-else Ladder:
I have used the if-else ladder to find the largest number of three numbers in C++.
#include <iostream> int main() { int num1, num2, num3; std::cout<<" Enter the number1 = "; std::cin >> num1; std::cout<<" Enter the number2 = "; std::cin >> num2; std::cout<<" Enter the number3 = "; std::cin >> num3; if (num1 > num2) { if (num1 > num3) { std::cout<< num1 << " is the largest number."<<std::endl; } else { std::cout<< num3 << " is the largest number."<<std::endl; } } else if (num2 > num3) { std::cout<< num2 << " is the largest number."<<std::endl; } else { std::cout<< num3 << " is the largest number."<<std::endl; } return 0; }
Using ternary conditions:
Besides using the if-else statement I am using here ternary conditions to find the largest number of three numbers in C++.
#include <iostream> int main() { int num1, num2, num3, tmp; std::cout<<" Enter the number1 = "; std::cin >> num1; std::cout<<" Enter the number2 = "; std::cin >> num2; std::cout<<" Enter the number3 = "; std::cin >> num3; tmp = (num1 > num2)? num1: num2; tmp = (tmp > num3) ? tmp : num3; std::cout<<" Largest number is " <<tmp<<std::endl; return 0; }
Using logical operator and ternary conditions:
You can also use the combination of logical operator and ternary condition to find the greatest of three numbers in C++.
#include <iostream> int main() { int num1, num2, num3, largest; std::cout<<" Enter the number1 = "; std::cin >> num1; std::cout<<" Enter the number2 = "; std::cin >> num2; std::cout<<" Enter the number3 = "; std::cin >> num3; largest =((num1>num2 && num1>num3)? num1: (num2>num3)? num2:num3); std::cout<<" Largest number is " << largest<<std::endl; return 0; }
Recommended Articles for you:
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- C++ Interview Questions with Answers.
- MCQs on C++
- C++ Variables, Constants, and literals.
- C++ Constructors, you should know.
- Compile Time Polymorphism with Templates in C++.
- Operator Overloading in C++ with some FAQ.
- Introduction of reference in C++.
- Use of mutable keywords in C++.
- List of some Best C++ Books, you must see.