fastread homefastrread library fastread menu

C++ : Find Largest Number Among Three Numbers

Tutorial by:Manisha Dubey      Date: 2016-06-18 04:12:39

❰ Previous Next ❱

In this program, user is asked to enter three numbers. Then this program finds out the largest number among three numbers entered by user and displays it with proper message. This program can be used in more than one way.

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;
}

Using if...else 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;
}

Using Nested if...else 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;
    }
    else if ((n2 >= n1) && (n2 >= n3)) {
        cout << "Largest number: " << n2;
    }
    else {
        cout << "Largest number: " << n3;
    }
    
    return 0;
}

The output of all three program is same.

 

 
 

Output

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

❰ Previous Next ❱


C++

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website