In this post, we will learn how to find the largest of three numbers using C++ Programming language.
This program asks the user to enter three integers, then it compares all three of them and find the largest number using the following methods:
- Using If Statement
- Using If. . .Else Statement
- Using Nested If. . .Else Statement
So, without further ado, let’s begin this tutorial.
C++ Program to Find Largest of Three Numbers
// C++ Program to Find Largest of Three Numbers #include <iostream> using namespace std; int main(){ int a, b, c; // Asking for input cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; cout << "Enter the third number: "; cin >> c; // Checking Largest number if (a > b && a > c){ cout << a << " is the largest number."; } if (b > a && b > c){ cout << b << " is the largest number."; } if (c > a && c > b){ cout << c << " is the largest number."; } return 0; }
Output
Enter the first number: 42
Enter the second number: 15
Enter the third number: 48
48 is the largest number.
How Does This Program Work ?
int a, b, c;
In this program, we have declared three int data type variables named a, b and c.
// Asking for input cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; cout << "Enter the third number: "; cin >> c;
Then, the user is asked to enter the value of all the three integers.
if (a > b && a > c){ cout << a << " is the largest number."; }
Now, we check whether a is greater than b and c or not. If yes, then a is the greatest number and we will print it.
if (b > a && b > c){ cout << b << " is the largest number."; }
If the above statement is false, then the program will check whether b is greater than a and c or not. If yes, then the program will print it otherwise it will move to check the next statement.
if (c > a && c > b){ cout << c << " is the largest number."; }
If c is greater than a and b, then clearly c is the greatest number and we’ll print it on the screen using the cout statement.
C++ Program to Find Largest of Three Numbers Using If. . .Else
// C++ Program to Find Largest of Three Numbers Using If. . .Else #include <iostream> using namespace std; int main(){ int p, q, r; // Asking for input cout << "Enter the first integer: "; cin >> p; cout << "Enter the second integer: "; cin >> q; cout << "Enter the third integer: "; cin >> r; // Comparing if ((p > q) && (p > r)){ cout << p << " is the greatest number."; } else if ((q > r) && (q > p)){ cout << q << " is the largest number."; } else{ cout << r << " is the largest number."; } return 0; }
Output
Enter the first integer: 21
Enter the second integer: 16
Enter the third integer: 28
28 is the largest number.
C++ Program to Find Largest of Three Numbers Using Nested If. . .Else
// C++ Program to Find Largest of Three Numbers Using If. . .Else #include <iostream> using namespace std; int main(){ int x, y, z; // Asking for input cout << "Enter the first number: "; cin >> x; cout << "Enter the second number: "; cin >> y; cout << "Enter the third number: "; cin >> z; // Largest of three numbers if (x > y){ if (x > z) cout << x << " is the greatest number."; else cout << z << " is the greatest number."; } else{ if (y > z) cout << y << " is the greatest number."; else cout << z << " is the greatest number."; } return 0; }
Output
Enter the first number: 12
Enter the second number: 20
Enter the third number: 8
20 is the greatest number.
Conclusion
I hope after going through this post, you understand how to find the largest of three numbers using C++ Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.
Also Read:
In the first output
What if I enter all equal numbers.
Then will it show that the greatest among 3 number is c.