Open In App

C++ Program to Find Largest Among Three Numbers

Last Updated : 14 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given three numbers, the task is to find the largest number among them in C++.

Examples

Input: a = 1, b = 2, c = 11
Output: 11
Explanation: Since 11 is the largest number among all numbers.

Input: a = 9, b = 7, c = 5
Output: 9
Explanation: Since 9 is the largest number among all numbers.

Find Largest Among Three Numbers in C++

The simplest approach is to use the if-else-if ladder to find the largest of three numbers. First, we find the larger number among two of the numbers. Then compare this larger number to the third number to find the largest.

Approach

Consider a, b and c to be the three numbers:

  • First, check if a is greater than both b and c. If yes, then a is the largest.
  • If a is not the largest, check if b is greater than or equal to c. If yes, then b is the largest.
  • If neither of these conditions is true, then c is the largest.

Code Implementation

C++
// C++ Program to find largest among three
// numbers using if-else-if ladder
#include <iostream>
using namespace std;

int main() {
    int a = 1, b = 2, c = 11;

    // Finding the largest by comparing using
  	// relational operators with if-else
    if (a >= b) {
        if (a >= c)
            cout << a;
        else
            cout << c;
    }
    else {
        if (b >= c)
            cout << b;
        else
            cout << c;
    }

    return 0;
}

Output
11

We can shorten this code by grouping multiple conditions in a single if statement using logical AND operator. (&&)

C++
// C++ program to find the maximum number among three
// numbers using compound expression in if-else
#include <iostream>
using namespace std;

int main() {
    int a = 1, b = 2, c = 11;

    // Finding largest using compound expressions
    if (a >= b && a >= c)
        cout << a;
    else if (b >= a && b >= c)
        cout << b;
    else
        cout << c;

    return 0;
}


Other Methods to Find Largest Among Three Numbers

Apart from if-else statement, there are also the other methods by which we can find the largest among three numbers in C++.

Using Inbuilt Function

In C++, the std::max() function can be used to find the largest value among the given set of values. Traditionally, it could only find the maximum of two numbers at once, however, starting from C++11, std::max() can also accept a set of values as initializer list (i.e. values inside braces {} ).

C++
// C++ Program to find the greatest of three 
// numbers using in-built max()
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a = 1, b = 2, c = 11;
  
  	// Find largest number among three numbers using
  	// std::max() function
    cout << max({a, b, c});

    return 0;
}

Output
11

Using Temporary Variable

We can also find the largest among three number using temporary variable. First, assume that one of the numbers is the largest and store it in a temporary variable. Then compare this temporary variable with remaining numbers and update it if the compared number is bigger. By the end, we will be left with the largest number in the temporary variable.

This method is the standard method for finding the maximum among the set of numbers if a built-in method is not available.

Code Implementation

C++
// C++ Program to find largest among three 
// numbers using temporary variable
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a = 1, b = 2, c = 11;

    // Assuming the maximum variable is a
    int temp = a;

    // Check if b is larger than temp or not
    if (b > temp)
        temp = b;

    // Check if c is larger than temp or not
    if (c > temp)
        temp = c;

    cout << temp;

    return 0;
}

Output
11




Next Article
Practice Tags :

Similar Reads