Find the Largest Number Using the In-built max() Function

C++ provides a library function named max() to find the maximum out of two numbers. It is defined inside the <algorithm> header file. We can use this function to find the maximum out of three numbers as shown in the below program.

C++ Program to Find the Maximum Among the Three Numbers using max() Function

C++




// C++ Program to find the
// greatest of three numbers
// using In-built max()
#include <algorithm>
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    int a, b, c;
    a = 10, b = 20, c = 30;
 
    int ans;
 
    ans = max(a, max(b, c));
    cout << ans << " is the largest";
 
    return 0;
}


Output

30 is the largest

Complexity Analysis

  • Time Complexity: O(1)
  • Auxiliary Space: O(1)

Related Articles



C++ Program to Find Largest Among Three Numbers

There are many different methods using which we find the maximum or largest number among three numbers. In this article, we will learn three such methods to find the largest among three numbers in C++.

Example:

Input: a = 1, b = 2, c = 45
Output: The Largest Among 3 is 45

Similar Reads

Methods to Find the Largest of Three Numbers

We can use the three methods to find the largest of the three numbers:...

1. Using if-else Statements

Algorithm...

2. Using Temporary Variable

...

3. Find the Largest Number Using the In-built max() Function

It is one of the simplest methods where we take a new variable equal to any of the three numbers and assume it to be max. The algorithm for this method is as follows:...