# C++ Program to Find Largest of Three Numbers

Howdy readers, today you will learn how to write a C++ program to find the largest of three numbers.

The below program prompts the user to enter three numbers, then it finds and prints the largest among the three using the following methods:

1. Using If Statement
2. Using If-Else Statement
3. Using Nested If-Else Statement
4. Using Ternary Operator

You are free to use any of the following methods. Each of the methods is very easy to understand.

So, without any delay, let’s begin this tutorial.

## C++ Program to Find Largest of Three Numbers Using If Statement

C++ Program

```// C++ Program to Find Largest of Three Numbers Using If Statement
#include <iostream>
using namespace std;

int main(){
int num1, num2, num3;

// Taking input
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter the third number: ";
cin >> num3;

// Find largest number
if (num1 > num2 && num1 > num3){
cout << "Largest number is: " << num1 << endl;
}
if (num2 > num1 && num2 > num3){
cout << "Largest number is: " << num2 << endl;
}
if (num3 > num1 && num3 > num2){
cout << "Largest number is: " << num3 <<endl;
}

return 0;
}
```

Output

```Enter the first number: 12
Enter the second number: 19
Enter the third number: 7
Largest number is: 19
```

Explanation

```    int num1, num2, num3;
```

In this program, we have declared three int data type variables named `num1`, `num2` and `num3`.

```    // Taking input
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter the third number: ";
cin >> num3;
```

Then, the program prompts the user to enter the values of three numbers. The entered values get stored in the `num1`, `num2` and `num3` named variables respectively.

```    if (num1 > num2 && num1 > num3){
cout << "Largest number is: " << num1 << endl;
}
```

Now, we check whether the value of `num1` is greater than both `num2` and `num3` or not. If yes, then `num1` is the largest number and we display it on the screen using the cout statement.

```    if (num2 > num1 && num2 > num3){
cout << "Largest number is: " << num2 << endl;
}
if (num3 > num1 && num3 > num2){
cout << "Largest number is: " << num3 <<endl;
}
```

Similarly, if num1 is not the greatest number, then we use the same logic to check for `num2` and `num3`.

The largest among the three numbers is displayed on the screen.

## C++ Program to Find Largest of Three Numbers Using If-Else

C++ Program

```// C++ Program to Find Largest of Three Numbers Using If-Else
#include <iostream>
using namespace std;

int main(){
int a, b, c;

// Taking input
cout << "Enter the first number: ";
cin >> a;
cout << "Enter the second number: ";
cin >> b;
cout << "Enter the third number: ";
cin >> c;

// Find largest among three
if ((a >= b) && (a >= c)){
cout << "Largest number: " << a;
}
else if ((b >= a) && (b >= c)){
cout << "Largest number: " << b;
}
else{
cout << "Largest number: " << c;
}

return 0;
}
```

Output

```Enter the first number: 25
Enter the second number: 17
Enter the third number: 42
Largest number: 42
```

## C++ Program to Find Largest of Three Numbers Using Nested If-Else

C++ Program

```// C++ Program to Find Largest of Three Numbers Using Nested If-Else
#include <iostream>
using namespace std;

int main(){
int x, y, z;

// Taking input
cout << "Enter the first number: ";
cin >> x;
cout << "Enter the second number: ";
cin >> y;
cout << "Enter the third number: ";
cin >> z;

// Largest using nested if-else
if (x >= y){
if (x >= z){
cout << "Largest number is: " << x;
}
else{
cout << "Largest number is: " << z;
}
}

else{
if (y >= z){
cout << "Largest number is: " << y;
}
else{
cout << "Largest number is: " << z;
}
}

return 0;
}
```

Output

```Enter the first number: 15
Enter the second number: 8
Enter the third number: 7
Largest number is: 15
```

## C++ Program to Find Largest of Three Numbers Using Ternary Operator

C++ Program

```// C++ Program to Find Largest of Three Numbers Using Ternary Operator
#include <iostream>
using namespace std;

int main(){
int a, b, c, largest;

// Taking input
cout << "Enter the first number: ";
cin >> a;
cout << "Enter the second number: ";
cin >> b;
cout << "Enter the third number: ";
cin >> c;

// Largest using ternary operator
largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

// Display result
cout << "Largest number is: " << largest;

return 0;
}
```

Output

```Enter the first number: 11
Enter the second number: 18
Enter the third number: 32
Largest number is: 32
```

Explanation

```    int a, b, c, largest;
```

In this program, we have declared four int data type variables named `a`, `b`, `c` and `largest`.

```    largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
```

Then, we use a ternary operator to find the largest number. Ternary operator takes three arguments:

1. Condition
2. Value to return when condition is met
3. Value to return when condition is not met

If `(a > b)` is true, then it proceeds to check the condition `(a > c)`. If the condition is met, then `a` is the largest otherwise `c` is the largest.

If `(a > b)` is false, then it proceeds to check the condition `(b > c)`. If the condition is met, then `b` is the largest otherwise `c` is the largest.

The largest number gets stored in the `largest` named variable.

```    cout << "Largest number is: " << largest;
```

And, we print the largest number on the screen using the `cout` statement.

## Conclusion

Today you learned how to write a C++ program to find the largest of three numbers.

If you have any doubts regarding the tutorial, then comment down your queries in the comment section.