Find The Largest Among Three Numbers In C++

What we are going to see through this article is we will see in detail Find The Largest Among Three Numbers In C++. here are three methods available to find the largest number among three numbers in C++ language. Following are the three methods.

  • Using if statement
  • Using if … else statement
  • Using Nested if statement
Find The Largest Among Three Numbers In C++

Type 1: Find the Largest Among Three Numbers Using if Statement

#include <iostream>
using namespace std;

int main() {    
    float n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    if(n1 >= n2 && n1 >= n3)
        cout << "Largest number: " << n1;

    if(n2 >= n1 && n2 >= n3)
        cout << "Largest number: " << n2;
    
    if(n3 >= n1 && n3 >= n2)
        cout << "Largest number: " << n3;
  
    return 0;
}

Output

Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3

Type 2 : Find the Largest Among Three Numbers Using if…Else Statement

Two if..else condition is used to check which number is largest among the three numbers.

#include <iostream>
using namespace std;

int main() {
    float n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    if((n1 >= n2) && (n1 >= n3))
        cout << "Largest number: " << n1;
    else if ((n2 >= n1) && (n2 >= n3))
        cout << "Largest number: " << n2;
    else
        cout << "Largest number: " << n3;
    
    return 0;
}

Output

Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3

Read Also: Remove Punctuations From A String In C++

Type 3 : Find the Largest Among Three Numbers Using Nested if Statement

#include <iostream>
using namespace std;

int main() {
    float n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    if (n1 >= n2) {
        if (n1 >= n3)
            cout << "Largest number: " << n1;
        else
            cout << "Largest number: " << n3;
    }
    else {
        if (n2 >= n3)
            cout << "Largest number: " << n2;
        else
            cout << "Largest number: " << n3;
    }

    return 0;
}

Output

Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3

Final Words

What we learned through this article is to Find The Largest Among Three Numbers In C++. Also if you have any doubts please leave a comment via the comment box. And we ask that you benefit everyone who shared this article with your friends.

Leave a Comment