C++ for Beginners: C++ Program to Find Largest Number Among Three Numbers

Hits: 9

(C++ programming Example for Beginners)

C++ Program to Find Largest Number Among Three Numbers

In this example, you’ll learn to find the largest number among three numbers using if, if else and nested if else statements.

To understand this example, you should have the knowledge of the following C++ programming topics:

  • C++ if, if…else and Nested if…else

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.


Example 1: Find Largest Number 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

Example 2: Find Largest Number 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) && (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

Example 3: Find Largest Number 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) {
        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

 

 

C++ for Beginners: C++ Program to Find Largest Number Among Three Numbers

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.