- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Find Largest Number Among Three Numbers
The largest number among three numbers can be found using if statement multiple times. This is given in a program as follows −
Example
#include <iostream> using namespace std; int main() { int a = 5 ,b = 1 ,c = 9; if(a>b) { if(a>c) cout<<a<<" is largest number"; else cout<<c<<" is largest number"; }else { if(b>c) cout<<b<<" is largest number"; else cout<<c<<" is largest number"; } return 0; }
Output
9 is largest number
In the above program, firstly, a is compared to b. If a is greater than b, then it is compared to c. If it is greater than c as well, that means a is the largest number and if not, then c is the largest number.
if(a>b) { if(a>c) cout<<a<<" is largest number"; else cout<<c<<" is largest number"; }
If a is not greater than b, that means b is greater than a. Then b is compared to c. If it is greater than c, that means b is the largest number and if not, then c is the largest number.
else { if(b>c) cout<<b<<" is largest number"; else cout<<c<<" is largest number"; }
Advertisements