In this C++ tutorial, you will learn how to write a C++ program to find the largest or maximum of three numbers. The examples use if...else, nested if, the ternary operator, and the standard std::max() function. We shall also consider equal values so that the comparison logic works when two or all three numbers are the same.

C++ Program to Find the Maximum of Three Numbers

Given three numbers a, b, and c, the task is to determine which value is greater than or equal to the other two. For example, if the numbers are 11, 55, and 23, the maximum value is 55.

There are several ways to express this comparison in C++. The most suitable approach depends on whether you are learning conditional statements, writing compact expressions, or using the standard library.

1. Find Maximum of Three Numbers Using If-Else-If

In this example program, we shall use C++ If Else If statement to find the maximum of three numbers.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int a = 11;
   int b = 55;
   int c = 23;

   int max;

   if (a>b && a>c) {
      max = a;
   } else if (b>c) {
      max = b;
   } else {
      max = c;
   }
   
   cout << max << endl;
}

Output

55

The first condition checks whether a is greater than both b and c. Since it is false, execution moves to the else if condition. The expression b > c is true for 55 > 23, so b is assigned to max.

2. Find Maximum of Three Numbers Using Nested If-Else

In this example program, we shall use C++ Nested If Else statement to find the maximum of three numbers.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

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

   int max;

   if (a > b) {
      if (a > c) {
         max = a;
      } else {
         max = c;
      }
   } else {
      if (b > c) {
         max = b;
      } else {
         max = c;
      }
   }
   
   cout << max << endl;
}

Output

11

Here, the outer if first compares a with b. Because 11 > 5, the inner condition compares a with c. Since 11 > 2, the value of a becomes the maximum.

3. Find Maximum of Three Numbers Using Simple If Statements

In this example program, we shall use simple C++ If statement to find the maximum of three numbers.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int a = 11;
   int b = 5;
   int c = 32;

   int max;

   if (a > b && a > c) {
      max = a;
   }
   if (b > a && b > c) {
      max = b;
   }
   if (c > a && c > b) {
      max = c;
   }
   
   cout << max << endl;
}

Output

11

For the values in this program, c is 32 and is greater than both a and b. Therefore, the value computed by the program is 32. The existing output block above does not match the values used in the program.

There is another limitation with this particular approach: all three tests use the strict > operator. If the maximum value occurs more than once, such as 10, 10, 5, none of the corresponding strict comparisons is true and max may remain uninitialized. A comparison that deliberately handles equal values is safer.

Handle Equal Values When Finding the Maximum of Three Numbers

A maximum-finding program should also work when two or all three inputs are equal. One straightforward solution is to use >= in the comparisons.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int a = 20;
    int b = 20;
    int c = 12;
    int max;

    if (a >= b && a >= c) {
        max = a;
    } else if (b >= a && b >= c) {
        max = b;
    } else {
        max = c;
    }

    cout << max << endl;
}

Output

20

Both a and b contain the maximum value. The program does not need to identify which variable is uniquely largest; it only needs to produce the maximum value, which is 20.

4. Find Maximum of Three Numbers Using Ternary Operator

In this example program, we shall use C++ Ternary Operator to find the maximum of three numbers.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int a = 11;
   int b = 5;
   int c = 23;

   int max = (a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c) ;
   
   cout << max << endl;
}

Output

23

The outer ternary expression first compares a and b. It then evaluates one of the inner ternary expressions to compare the larger candidate with c. In this example, 23 is selected as the maximum.

A nested ternary expression is compact, but an if...else version may be easier to read when the comparison logic becomes more complicated.

Find Maximum of Three Numbers Using std::max()

C++ provides std::max() in the <algorithm> header. The two-argument form returns the greater of two values. To compare three numbers, one call can be nested inside another.

</>
Copy
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int a = 11;
    int b = 55;
    int c = 23;

    int largest = max(max(a, b), c);

    cout << largest << endl;
}

Output

55

The inner call max(a, b) first returns the greater of a and b. The outer call then compares that result with c. This approach naturally handles equal values as well.

Find the Maximum of Three User-Entered Numbers in C++

The earlier examples use fixed values in the source code. The following version reads three numbers from standard input and then finds the maximum.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int a, b, c;

    cout << "Enter three numbers: ";
    cin >> a >> b >> c;

    int largest;

    if (a >= b && a >= c) {
        largest = a;
    } else if (b >= a && b >= c) {
        largest = b;
    } else {
        largest = c;
    }

    cout << "Maximum = " << largest << endl;
}

Example Input and Output

Enter three numbers: 14 38 27
Maximum = 38

Find Maximum of Three Numbers Using a C++ Function

If the same comparison is needed in several places, it can be placed in a function. The function receives three numbers and returns their maximum value.

</>
Copy
#include <iostream>
using namespace std;

int maximumOfThree(int a, int b, int c) {
    if (a >= b && a >= c) {
        return a;
    } else if (b >= a && b >= c) {
        return b;
    }

    return c;
}

int main() {
    cout << maximumOfThree(42, 17, 31) << endl;
}

Output

42

This form separates the maximum-finding logic from the rest of the program and makes it reusable for different sets of three integer values.

Choosing a Method to Find the Largest of Three Numbers in C++

  • If-else-if: suitable when learning conditional statements and easy to extend with additional logic.
  • Nested if-else: demonstrates step-by-step comparisons but introduces additional nesting.
  • Separate if statements: can work, but strict comparisons need special care when values are equal.
  • Ternary operator: compact for a small expression, though nested ternary expressions can become harder to read.
  • std::max(): concise when the goal is simply to obtain the maximum value.
  • Function: useful when the same maximum-of-three operation must be reused.

C++ Maximum-of-Three Comparison Checks

  • Test a case where the first number is largest, such as 30, 20, 10.
  • Test a case where the second number is largest, such as 10, 30, 20.
  • Test a case where the third number is largest, such as 10, 20, 30.
  • Test equal maximum values, such as 30, 30, 10.
  • Test all three values equal, such as 15, 15, 15.
  • Test negative values, such as -8, -2, -12, where -2 is the maximum.
  • Make sure a variable storing the result is assigned on every possible execution path before it is printed.

Summary of Finding the Maximum of Three Numbers in C++

To find the maximum of three numbers, compare each candidate with the other values and select the greatest value. An if...else-if chain is a clear approach for learning the comparison logic, while std::max() provides a concise standard-library solution. When writing the comparisons yourself, account for equal values by using logic such as >= where appropriate.

In this C++ Tutorial, we learned how to find the maximum number of given three numbers using different conditional statements in C++.