C++ Program to Find Largest Among Three Numbers

In this tutorial, you will see how to find the largest among three numbers using a C++ program.

Example,

Input: num1= 12, num2 = 108, num3= 100
Output: Largest number = 108

Input: num1= 200, num2 = 180, num3= 10
Output: Largest number = 200

Below mentioned C++ program asks the user to enter three integers numbers, then it compares all three numbers together to find the largest number using the mentioned methods:

 

General logic to find the largest number of three given numbers:

  1. C++ program asks to enter three integer numbers.
  2. Now compare all three numbers together using any of the mentioned methods. But here I am considering nested if-else for understanding the concept.
  3. Compare num1 with num2.
  4. If num1 is greater than num2, then check if num1 is greater than num3.
    1. If true, then print ‘num1’ as the greatest number.
    2. If false, then print ‘num3’ as the greatest number.
  5. If num2 is greater than num1, then check if num2 is greater than num3.
    1. If true, then print ‘num2’ as the greatest number.
    2. If false, then print ‘num3’ as the greatest number.

 

Flow Chart:

Find largest of three given numbers in c++

 

 

Using the logical operator and if condition:

I have used the logical operator and if statement to find the largest number of three numbers in C++.

#include <iostream>


int main()
{
    int num1, num2, num3;

    std::cout<<" Enter the number1 = ";
    std::cin >> num1;

    std::cout<<" Enter the number2 = ";
    std::cin >> num2;

    std::cout<<" Enter the number3 = ";
    std::cin >> num3;


    if (num1 >= num2 && num1 >= num3)
    {
        std::cout<< num1 << "is the largest number."<<std::endl;
    }
    if (num2 >= num1 && num2 >= num3)
    {
        std::cout<< num2 << "is the largest number."<<std::endl;
    }
    if (num3 >= num1 && num3 >= num2)
    {
        std::cout<< num3 << " is the largest number."<<std::endl;
    }

    return 0;
}

 

Using the nested if-else:

I have used the nested if-else statement to find the largest number of three numbers in C++.

#include <iostream>

int main()
{
    int num1, num2, num3;

    std::cout<<" Enter the number1 = ";
    std::cin >> num1;

    std::cout<<" Enter the number2 = ";
    std::cin >> num2;

    std::cout<<" Enter the number3 = ";
    std::cin >> num3;


    if (num1 >= num2)
    {
        if (num1 >= num3)
        {
            std::cout<< num1 << "is the largest number."<<std::endl;
        }
        else
        {
            std::cout<< num3 << "is the largest number."<<std::endl;
        }
    }
    else
    {
        if (num2 >= num3)
        {
            std::cout<< num2 << " is the largest number."<<std::endl;
        }
        else
        {
            std::cout<< num3 << "is the largest number."<<std::endl;
        }
    }

    return 0;
}

 

 

Using the if-else Ladder:

I have used the if-else ladder to find the largest number of three numbers in C++.

#include <iostream>

int main()
{
    int num1, num2, num3;

    std::cout<<" Enter the number1 = ";
    std::cin >> num1;

    std::cout<<" Enter the number2 = ";
    std::cin >> num2;

    std::cout<<" Enter the number3 = ";
    std::cin >> num3;


    if (num1 > num2)
    {
        if (num1 > num3)
        {
            std::cout<< num1 << " is the largest number."<<std::endl;
        }
        else
        {
            std::cout<< num3 << " is the largest number."<<std::endl;
        }
    }
    else if (num2 > num3)
    {
        std::cout<< num2 << " is the largest number."<<std::endl;
    }
    else
    {
        std::cout<< num3 << " is the largest number."<<std::endl;
    }

    return 0;
}

 

Using ternary conditions:

Besides using the if-else statement I am using here ternary conditions to find the largest number of three numbers in C++.

#include <iostream>

int main()
{
    int num1, num2, num3, tmp;

    std::cout<<" Enter the number1 = ";
    std::cin >> num1;

    std::cout<<" Enter the number2 = ";
    std::cin >> num2;

    std::cout<<" Enter the number3 = ";
    std::cin >> num3;

    tmp = (num1 > num2)? num1: num2;
    tmp = (tmp > num3) ? tmp : num3;

    std::cout<<" Largest number is " <<tmp<<std::endl;

    return 0;
}

 

 

Using logical operator and ternary conditions:

You can also use the combination of logical operator and ternary condition to find the greatest of three numbers in  C++.

#include <iostream>

int main()
{
    int num1, num2, num3, largest;

    std::cout<<" Enter the number1 = ";
    std::cin >> num1;

    std::cout<<" Enter the number2 = ";
    std::cin >> num2;

    std::cout<<" Enter the number3 = ";
    std::cin >> num3;

    largest =((num1>num2 && num1>num3)? num1: (num2>num3)? num2:num3);

    std::cout<<" Largest number is " << largest<<std::endl;

    return 0;
}

 

Recommended Articles for you:

Leave a Reply

Your email address will not be published. Required fields are marked *