Find largest number among three

We will take three integers from the user and then find largest number among them. We can write the logic for this program in various way. Here we will find largest number using if else statement.

The process will be comparing the numbers with one another. We will compare each number among other two numbers to find that this number is largest or not.

C++ program to find largest number

At first we will take three integers from the user. You can also write the program for float number. Then we will compare them and find the largest number among them. Let’s see the source code bellow;

// find largest number program in c++

#include <iostream>
using namespace std;

int main(){
    int firstNum, secondNum, thirdNum;

    cout << "Enter first number here : ";
    cin >> firstNum;
    cout << "Enter second number here : ";
    cin >> secondNum;
    cout << "Enter third number here : ";
    cin >> thirdNum;

    if((firstNum >= secondNum) && (firstNum >= thirdNum)){
        cout << "\nFirst number is largest.\n";

    }else if((secondNum >= firstNum) && (secondNum >= thirdNum)){
        cout << "\nSecond number is largest.\n";

    }else{
        cout << "\nThird number is largest.\n";
    }

    return 0;
}

Output of find largest number program

find-largest-number-among-three-in-c

Find largest number using nested if else statement

Now, we will see a C++ program to find the largest number using nested if else statement. Let’s see the program.

// find largest number using nested if else

#include <iostream>
using namespace std;

int main(){
    float firstNumber, secondNumber, thirdNumber;

    cout <<"Enter three numbers to find largest one : \n";
    cin >> firstNumber >> secondNumber >> thirdNumber;

    if(firstNumber >= secondNumber){
        if(firstNumber >= thirdNumber){
            cout <<"\nFirst number is largest.\n";
        }else{
            cout <<"\nThird number is largest.\n";
        }

    }else{
        if(secondNumber >= thirdNumber){
            cout <<"\nSecond number is largest.\n";
        }else{
            cout <<"\nThird number is largest.\n";
        }
    }

    return 0;
}

Program output

Enter three numbers to find largest one :
5 7 9

Third number is largest.
Enter three numbers to find largest one :
230 500 1200

Third number is largest.
Enter three numbers to find largest one :
5 8 3

Second number is largest.

Previous pageVowel or consonant

Next pageLeap year program