Open In App

C++ Program to Find Largest Among Three Numbers

Last Updated : 02 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

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 Statement.
  2. Using Temporary Variable
  3. Using In-built max() Function.

1. Using if-else Statements

Algorithm

  1. Start.
  2. Input a, b, and c.
  3. Check the condition a>=b
  4. If step 3 is True go to step 5 else go to step 9.
  5. Check the condition a>=c.
  6. If step 5 is True go to step 7 else go to step 8.
  7. Print “The Largest Among 3 is: a and go to step 13
  8. Print “The Largest Among 3 is: c and go to step 13.
  9. Check the condition b>=c.
  10. If step 9 is True go to step 11 else go to step 12.
  11. Print “The Largest Among 3 is: b and go to step 13.
  12. Print “The Largest Among 3 is: c and go to step 13.
  13. Stop.

C++ Program to Find the Largest Number using if-else.

C++




// C++ Program to Find Largest Among
// Three Numbers Using if-else
// Statement
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    int a, b, c;
    cout << "Enter the three numbers a, b & c" << endl;
    cin >> a >> b >> c;
 
    if (a >= b) {
        // If 'a' is greater than or equal to 'b', compare
        // 'a' with 'c'
        if (a >= c) {
            // If 'a' is also greater than or equal to 'c',
            // it is the largest number
            cout << "The Largest Among Three Numbers is : "
                 << a << endl;
        }
        else {
            // If 'a' is not greater than or equal to 'c',
            // 'c' must be the largest number
            cout << "The Largest Among Three Numbers is : "
                 << c << endl;
        }
    }
    else {
        // If 'b' is greater than 'a', compare 'b' with 'c'
        if (b >= c) {
            // If 'b' is also greater than or equal to 'c',
            // it is the largest number
            cout << "The Largest Among Three Numbers is : "
                 << b << endl;
        }
        else {
            // If 'b' is not greater than or equal to 'c',
            // 'c' must be the largest number
            cout << "The Largest Among Three Numbers is : "
                 << c << endl;
        }
    }
    return 0;
}


Output

Enter the three numbers a, b & c
The Largest Among Three Numbers is : 4196384

Complexity Analysis

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

2. Using Temporary Variable

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:

Algorithm

  1. Create a new variable named max = a.
  2. If max is less than b, then max = b.
  3. If max is less than c, then max = c.
  4. Return max.

C++ Program to Find the Largest Among Three Numbers Using Temporary Variable

C++




// C++ program to find largest among three numbers using
// temporary variable
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    int a = 1, b = 10, c = 4;
 
    // temporary variable where we assumed a is max
    int max = a;
 
    if (max < b)
        max = b;
    if (max < c)
        max = c;
 
    printf("%d is the maximum out of %d, %d, and %d", max,
           a, b, c);
 
    return 0;
}


Output

10 is the maximum out of 1, 10, and 4

Complexity Analysis

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

3. 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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads