C++ Program to Find Largest or Greatest Number Among Three Numbers (5 Ways)

In this program, we learn how to find the Largest or Greatest Number Among Three Numbers using the C++ Programming Language.

We will use Five ways to write code for it.

  1. Using if statement
  2. Using if-else statement
  3. Using nested if-else statement
  4. Using ternary operator
  5. Using max() function

Let's discuss them one by one.

C++ Programming Code to Find the Largest or Greatest Number Among Three Numbers

Using if statement

Code:-

#include <iostream>

using namespace std;

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

    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter second number: ";
    cin >> num2;
    cout << "Enter third number: ";
    cin >> num3;  

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

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

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

    return 0;
}

Output:-

Enter first number: 84
Enter second number: 69
Enter third number: 73
Largest number: 84

Using if-else statement

Code:-

#include <iostream>

using namespace std;

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

    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter second number: ";
    cin >> num2;
    cout << "Enter third number: ";
    cin >> num3;  

    if((num1 >= num2) && (num1 >= num3))
        cout << "Greatest number: " << num1 << endl;
    else if ((num2 >= num1) && (num2 >= num3))
        cout << "Greatest number: " << num2 << endl;
    else
        cout << "Greatest number: " << num3 << endl;
    
    return 0;
}

Output:-

Enter first number: 746
Enter second number: 345
Enter third number: 842
Greatest number: 842

Using nested if-else statement

Code:-

#include <iostream>

using namespace std;

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

    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter second number: ";
    cin >> num2;
    cout << "Enter third number: ";
    cin >> num3; 

    if (num1 >= num2)
    {
        if (num1 >= num3)
            cout << "Largest number: " << num1 << endl;
        else
            cout << "Largest number: " << num3 << endl;
    }
    else
    {
        if (num2 >= num3)
            cout << "Largest number: " << num2 << endl;
        else
            cout << "Largest number: " << num3 << endl;
    }

    return 0;
}

Output:-

Enter first number: 874
Enter second number: 315
Enter third number: 956
Largest number: 956

Using ternary operator

Code:-

#include <iostream>

using namespace std;

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

    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter second number: ";
    cin >> num2;
    cout << "Enter third number: ";
    cin >> num3; 
  
    float greatest = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3); 
  
    cout << "Greatest number: " << greatest << endl;
  
    return 0; 
}

Output:-

Enter first number: 49
Enter second number: 26
Enter third number: 73
Greatest number: 73

Using max() function

Code:-

#include <iostream>

using namespace std;

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

    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter second number: ";
    cin >> num2;
    cout << "Enter third number: ";
    cin >> num3; 
  
    float greatest = max(num1, max(num2, num3)); 
  
    cout << "Greatest number: " << greatest << endl;
  
    return 0; 
}

Output:-

Enter first number: 19
Enter second number: 64
Enter third number: 35
Greatest number: 64

You can learn about many other C++ Programs Here.







The following two tabs change content below.
Amit Rawat

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...