Advertisement

C++ Program to find the largest of three numbers using nested if


In this example, You will learn how to find the largest of three numbers using nested if statement in C++.


List is :10 20 40 => 40

List is :30 20 10 => 30

Example: How to find the largest of three numbers using nested if statement in C++

#include<iostream>
using namespace std;

int main() {

    int a, b, c;

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

    if (a > b) {
        if (a > c) {
            cout << "Largest is:" << a;
        } else {
            cout << "Largest is:" << c;
        }
    } else {
        if (b > c) {
            cout << "Largest is:" << b;
        } else {
            cout << "Largest is:" << c;
        }
    }

    return 0;
}

Output:

Enter three numbers:20 10 30
Largest is:30