Open In App

C++ Program to Find Largest Among Four Numbers

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to find the largest among four numbers in a C++ program.

Examples:

Input: a = 1, b = 2, c = 4, d = 3

Output: 4

Input: a = 0, b = -1, c = -3, d = -2

Output: 0

Methods to Find the Largest of Four Numbers

There are 4 ways to find the largest among the four numbers in C++:-

  1. Using If-else Statement
  2. Using Nested If-else
  3. Using Ternary Operator
  4. Using Multiple Max Function

1. Using if-else Statement

In this method, we will use the if-else ladder to check each of the numbers for the largest using compound relational expressions. If after checking three numbers, there is no largest, then we can be sure that the last number will be largest.

Algorithm

  1. Compare a with b, c and d. If a is greatest, print a.
  2. Else compare b with a, c and d. If b is greatest, print b.
  3. Else compare c with a, b and d. If c is greatest, print c.
  4. Else d is greatest, print d.

Program to Check Largest Number among Four Number using if-else

C++




// C++ program to find the largest number usign if else
#include <iostream>
using namespace std;
 
int main()
{
    // defining four numbers
    int a = 1, b = 2, c = 4, d = 3;
 
    // checking if a is largest
    if (a >= b && a >= c && a >= d)
        cout << "Largest Number: " << a;
 
    // checking if b is largest
    else if (b >= a && b >= c && b >= d)
        cout << "Largest Number: " << b;
 
    // checking if c is largest
    else if (c >= a && c >= b && c >= d)
        cout << "Largest Number: " << c;
 
    // d is largest
    else
        cout << "Largest Number: " << d;
 
    return 0;
}


Output

Largest Number: 4

2. Using Nested if-else

In this approach, instead of checking each number using compound expressions, we will compare two numbers, and if one is greater, then inside that if statement, we will compare this number with another number. We will keep nesting if-statements till the number is compared with all the other numbers.

Algorithm

  1. Compare a and b. If a is greater, then
    1. Compare a and c. If a is greater, then
      1. Compare a and d. If a is greater, print a.
      2. Else print d.
    2. Else,
      1. Compare c and d. If c is greater, print c.
      2. Else print d.
  2. Else,
    1. Compare b and c. If b is greater, then
      1. Compare b and d. If b is greater, then
        1. Print b.
      2. Else print d.
    2. Else,
      1. Compare c and d. If c is greater, print c.
      2. Else print d.

Program to Check Largest Number among Four Number using Nested if-else

C++




// C++ Program to Check Largest Number among Four Number using Nested if-else
#include <iostream>
using namespace std;
int main()
{
    double a = 1, b = 2, c = 4, d = 3;
    double Max;
 
    if (a > b) {
        if (a > c) {
            if (a > d)
                Max = a;
            else
                Max = d;
        }
        else {
            if (c > d)
                Max = c;
            else
                Max = d;
        }
    }
    else {
        if (b > c) {
            if (b > d)
                Max = b;
            else
                Max = d;
        }
        else {
            if (c > d)
                Max = c;
            else
                Max = d;
        }
    }
 
    cout << Max << endl;
 
    return 0;
}


Output

4

3. Using Ternary Operator

Ternary operators are simply the replacement of if-else statements. In this approach, we compare numbers as the two pairs of two numbers. Then, the bigger numbers from each of these comparisons are compared and the greater number among them is the largest number among the four numbers.

Algorithm

  1. Greater among a and b is Max1.
  2. Greater among c and d is Max2.
  3. Greater among Max1 and Max2 is Max3.
  4. Print Max3 as it is greatest.

Program to Check Largest Number among Four Number using Ternary Operator

C++




// C++ program  to Check Largest Number among Four Number
// using Ternary Operator
#include <iostream>
using namespace std;
int main()
{
    double a = 1, b = 2, c = 4, d = 3;
    double Max1, Max2, Max3;
 
    // bigger number among a and b
    Max1 = (a > b) ? a : b;
    // bigger number among c and d
    Max2 = (c > d) ? c : d;
 
    // comparing the numbers from first and second
    // comparison to get the max among all four numbers
    Max3 = (Max1 > Max2) ? Max1 : Max2;
 
    cout << Max3 << endl;
 
    // Another way but it is too long
    // cout << (a < b ? ((b > c)? ((b > d)? b:d) :((c > d)?
    // c: d))  : ((a < c)? ((c > d)? c: d) : ((a > d) ? a :
    // d))) << endl;
 
    return 0;
}


Output

4


4. Using In-built std::max() Function

The C++ language provides a built-in std::max() function as a part of the STL <algorithm> library. This function take two arguments and returns the larger number among them. We can use this function to find the maximum number among four numbers.

Algorithm

The algorithm of this method is same as that of method 3 but we are using max() function instead of ternary operator.

  1. First argument of max() is max(a, b) i.e. greater among a and b.
  2. Second Argument of max() is max(c, d) i.e greater among c and d.
  3. The max() function will return the largest among four of the numbers.

Program to Check Largest Number among Four Number using max() Function

C++




// C++ Program to Check Largest Number among Four Number
// using max() Function
#include <iostream>
using namespace std;
 
int main()
{
  // four numbers
    double a = 1, b = 2, c = 4, d = 3;
 
  // nesting max
    double Max = max(max(a, b), max(c, d));
 
    cout << Max << endl;
 
    return 0;
}


Output

4


C++ STL max() function can also take any number of arguments in the form of an initializer list. It means that we can put all four numbers in the max function as arguments

C++




// C++ Program to Check Largest Number among Four Number
// using a single call to max() function
#include <algorithm>
#include <iostream>
using namespace std;
 
int main()
{
    double a = 1, b = 2, c = 4, d = 3;
 
  // arguments are passed as initializer list
    double Max = max({a, b, c, d});
 
    cout << Max << endl;
 
    return 0;
}


Output

4



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads