C++ Program to Find Largest of Three Numbers

• Write a C++ program to find maximum of three numbers using if-else statement.
• How to find largest of three numbers using conditional operator.

First of all, we have to take three numbers as input from user and compares them to find the maximum of all three numbers.

C++ program to find maximum of three numbers using if else statement

In this program, we first finds largest of first two numbers and then compares it with third number.

```#include <iostream>

using namespace std;

int main()  {
int a, b, c, max;
/*
* Take three numbers as input from user
*/
cout <<"Enter Three Integers\n";
cin >> a >> b >> c;

if(a > b){
// compare a and c
if(a > c)
max = a;
else
max = c;
} else {
// compare b and c
if(b > c)
max = b;
else
max = c;
}

/* Print Maximum Number */
cout << "Maximum Number is = " << max;

return 0;
}
```
Output
```Enter Three Integers
8 2 6
Maximum Number is =  8
```

C++ Program to find maximum of three numbers using conditional or ternary operator

Let A, B and C are three input numbers. We first find largest of A and B. Lets say A > B then we will compare A and C to find the largest of all three numbers. We are going to use conditional operator here, which is similar to IF-THEN-ELSE statement.

```#include <iostream>

using namespace std;

int main()  {
int a, b, c, max;
/*
* Take three numbers as input from user
*/
cout << "Enter Three Integers\n";
cin >> a >> b >> c;

max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

/* Print Maximum Number */
cout << "Maximum Number is = " << max;

return 0;
}
```
Output
```Enter Three Integers
7 12 9
Maximum Number is = 12
```

Recommended Posts
 C++ Program to Find Average of Numbers Using Arrays C++ Program to Find Largest Element of an Array C++ Program to Find Smallest Element in Array C++ Program to Swap Two Numbers C++ Program to Find Quotient and Remainder C++ Program to Swap Numbers in Cyclic Order C++ Program to Find LCM and GCD of Two Numbers C++ Program to Find Sum of Natural Numbers C++ Program to Reverse Digits of a Number C++ Program to Convert Decimal Number to Octal Number C++ Program to check Whether a Number is Palindrome or Not All C++ Programs
 Previous Next