C++ Program To Find Greatest Of Four Numbers - C++ Programming Tutorial
C++ Course / C++ Programs / C++ Program To Find Greatest Of Four Numbers

C++ Program To Find Greatest Of Four Numbers

BLUF: Mastering C++ Program To Find Greatest Of Four Numbers is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Program To Find Greatest Of Four Numbers

C++ is renowned for its efficiency. Learn how C++ Program To Find Greatest Of Four Numbers enables low-level control and high-performance computing in the tutorial below.

In this guide, we are going to create a C++ code to determine the largest value among four given numbers.

For example

a = 10, b = 50, c = 20, d = 25

The greatest number is b 50

a = 35, b = 50, c = 99, d = 2

The greatest number is c 99

Approach 1

The method follows the conventional technique of identifying the largest value among four numbers. The initial if statement verifies if a is the largest, followed by subsequent if-else statements to evaluate b and c. Finally, the last else statement is executed to display d as the greatest number.

Algorithm

  • START
  • INPUT FOUR NUMBERS A, B, C, D
  • IF A > B THEN IF A > C THEN IF A > D THEN A IS THE GREATEST ELSE D IS THE GREATEST
  • ELSE IF B > C THEN IF B > D THEN B IS THE GREATEST ELSE D IS THE GREATEST
  • ELSE IF C > D THEN C IS THE GREATEST
  • ELSE D IS THE GREATEST

C++ Code

Example

#include <bits/stdc++.h>
using namespace std;
void find_greatest(int a, int b, int c, int d)
{
    if (a > b) {
        if (a > c) {
            if (a > d) {
                cout << "a is greatest";
            }
            else {
                cout << "d is greatest";
            }
        }
    }
    else if (b > c) {
        if (b > d) {
            cout << "b is greatest";
        }
        else {
            cout << "d is greatest";
        }
    }
    else if (c > d) {
        cout << "c is greatest";
    }
    else {
        cout << "d is greatest";
    }
}

int main()
{
    int a = 10, b = 50, c = 20, d = 25;
    cout << "a=" << a << " b=" << b << " c=" << c << " d=" << d;
    cout << "\n";
    find_greatest(a, b, c, d);
    a = 35, b = 50, c = 99, d = 2;
    cout << "\n";
    cout << "a=" << a << " b=" << b << " c=" << c << " d=" << d;
    cout << "\n";
    find_greatest(a, b, c, d);

    return 0;
}

Output

Output

a=10 b=50 c=20 d=25
b is greatest
a=35 b=50 c=99 d=2
c is greatest

Approach 2

This approach uses the inbuilt max function.

Here is the syntax of max function

Define a constexpr function template max that returns a reference to the constant value T, taking constant references a and b as arguments.

Here, a and b are the numbers to be compared.

Return: Larger of the two values.

For example

std :: max(2,5) will return 5

In order to determine the highest value among 4 numbers, we can employ a series of chained max functions like so -

int x = max(a, max(b, max(c, d)));

C++ code

Example

#include <bits/stdc++.h>
using namespace std;
void find_greatest(int a, int b, int c, int d)
{
    int x = max(a, max(b, max(c, d)));
    if (x == a)
        cout << "a is greatest";
    if (x == b)
        cout << "b is greatest";
    if (x == c)
        cout << "c is greatest";
    if (x == d)
        cout << "d is greatest";
}

int main()
{
    int a = 10, b = 50, c = 20, d = 25;
    cout << "a=" << a << " b=" << b << " c=" << c << " d=" << d;
    cout << "\n";
    find_greatest(a, b, c, d);
    a = 35, b = 50, c = 99, d = 2;
    cout << "\n";
    cout << "a=" << a << " b=" << b << " c=" << c << " d=" << d;
    cout << "\n";
    find_greatest(a, b, c, d);

    return 0;
}

Output

Output

a=10 b=50 c=20 d=25
b is greatest
a=35 b=50 c=99 d=2
c is greatest

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience