# Write a Program to Find the Greatest of Three Numbers in C++

In this post, we will write a program to find the greatest of three numbers in C++. First, we will use the if-else statement, and then we will use a switch case. Using if-else there are multiple ways to find the greatest of three numbers in C++. We will also write the C++ program to find the largest of three numbers using switch case.

Compare first and second number,

• If the first number is bigger then compare the first number with the third number. Now, if the first number is greater then it is the biggest number else the third number is the biggest.
• But if the second number is bigger then compare the second and third numbers. Here, if the second number is bigger then the second number is the biggest number among them else the third number is the biggest among them.

C++ Program to find largest among three number using nested If-Else statements

``````#include<iostream>
using namespace std;
int main()
{
// declare variables
double n1,n2,n3;

// take input
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;

// compare first and second number
if(n1 > n2)
{
// compare first and third number
if(n1 > n3)
cout << "Largest = " << n1 << endl;
else
cout << "Largest = " << n3 << endl;
}
else
{
// compare second and third number
if(n2 > n3)
cout << "Largest = " << n2 << endl;
else
cout << "Largest = " << n3 << endl;
}
return 0;
}``````

Output:-

Enter three numbers: 15 5 10
Largest = 15

## Program to Find the Greatest of Three Numbers in C++ Using Simple If Statements

Here, we check the numbers as,

• If the first number is greater than the remaining numbers then the first number is the greatest number.
• Similarly, if the second number is greater than the first and third number then it is the greatest among them.
• Similarly, if the third number is greater than remaining numbers then the third number is the greatest number among them.
``````#include<iostream>
using namespace std;
int main()
{
// declare variables
int x, y, z;

// take inputs
cout << "Enter Three Numbers: ";
cin >> x >> y >> z;

// when x is largest
if ( x >= y && x >= z )
cout << x <<" is largest Number." << endl;

// when Y is largest
if ( y >= x && y >= z )
cout << y << " is largest number." << endl;

// when z is largest
if ( z >= x && z >= y )
cout << y << " is largest number." << endl;

return 0;
}``````

This program with written using a simple if statement and AND (&&) operator. We can write the same program using an if-else statement, and in this case, we don’t need to write the last condition.

## Program to Find the Greatest of Three Numbers in C++ Using If-else Statement

``````#include<iostream>
using namespace std;
int main()
{
// declare variables
int x, y, z;

// take inputs
cout << "Enter Three Numbers: ";
cin >> x >> y >> z;

// when x is largest
if ( (x >= y) && (x >= z) )
cout << x <<" is largest Number." << endl;

// when Y is largest
else if ( (y >= x) && (y >= z) )
cout << y << " is largest number." << endl;

// else z will be largest
else
cout << y << " is the largest number." << endl;

return 0;
}``````

Output:-

Enter Three Integers: 10 12 9
12 is the largest number.

## Using Switch-Case

``````#include <iostream>
using namespace std;
int main()
{
// declare variables
double x,y,z;

// take inputs
cout << "Enter Three Numbers: ";
cin >> x >> y >> z;

switch (x > y)
{
// x is greater than y
case 1 :
switch(x > z)
{
// x is greater than z
case 1 :
cout << "Largest = " << x << endl;
break;
// z is greater than x
case 0 :
cout << "Largest = " << z << endl;
break;
}
break;
// y is greater than x
case 0 :
switch(y > z)
{
// y is greater than z
case 1 :
cout << "Largest = " << y << endl;
break;
// z is greater than y
case 0 :
cout << "Largest = " << z << endl;
break;
}
break;
}
return 0;
}``````

Output:-

Enter Three Numbers: 15 10 20
Largest = 20

Enter Three Numbers: -55.6 99.56 0.999
Largest = 99.56

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

### 2 thoughts on “Write a Program to Find the Greatest of Three Numbers in C++”

1. I am sure this piece of writing has touched all the internet users, its really really good piece of writing on building up a new weblog.

2. Hello, after reading this awesome article I am as well glad to share my know-how here with mates.