Checking For the Largest Number in C++

Exercise:

Write a C++ program to pick the largest number out of the three inserted numbers.

Method 1: Using If.. Statement to Pick the Largest Number:

Click Here to View the Solution!
#include <iostream>
using namespace std;
int main()
{    
    float num1, num2, num3;
    cout << "Insert 3 random numbers:\n ";
    cin >> num1 >> num2 >> num3;
    if(num1 >= num2 && num1 >= num3)
        cout << num1 <<" is the largest number ";
    if(num2 >= num1 && num2 >= num3)
        cout << num2 <<" is the largest number ";
    if(num3 >= num1 && num3 >= num2)
        cout << num3 <<" is the largest number ";
    return 0;
}

Click Here to View the Output!
Enter three numbers:
 23
76
34
76 is the largest number
Click Here to View the Explanation!
  • 3 variables num1, num2, and num3 are declared as float.
  • 3 if statements are added evaluating 1 by 1 which number is greater
  • For example, (num1 >= num2 && num1 >= num3) indicated that if the first number is greater or equal to second number, and the first number is greater than or equal to the third number, the first number was true.
  • If num1 is 23, num2 is 76, and num3 is 34, the code evaluates if 23 is greater than 76 and 34. If not, it moves on to the second statement checking if 76 is greater than 23 and 34. Since the statement is marked true, the output displays that 76 is the greatest number.

Method 2: Using If… else Statement to Pick the Largest Number:

Click Here to View the Solution!
#include <iostream>
using namespace std;
int main() 
{
    float num1, num2, num3;
    cout << "Enter three numbers:\n ";
    cin >> num1 >> num2 >> num3;
    if((num1 >= num2) && (num1 >= num3))
        cout << num1 <<" is the largest number ";
    else if ((num2 >= num1) && (num2 >= num3))
        cout << num2 <<" is the largest number ";
    else
        cout << num3 <<" is the largest number ";
    return 0;
}
Click Here to View the Output!
Enter three numbers:
  -43
 5.65
 -16
 5.65 is the largest number
Click Here to View the Explanation!
  • This method uses if... else statements.
  • The entered numbers are checked 1 by 1 through the statement like the previous example. However, the last statement is not required because if the previous two statements are marked false, the compiler will automatically move to the third statement and display an output accordingly.

Method 3: Using Nested if… else Statement to Pick the Largest Number:

Click Here to View the Solution!
#include <iostream>
using namespace std;
int main() 
{
    float num1, num2, num3;
    cout << "Enter three numbers:\n ";
    cin >> num1 >> num2 >> num3;
    if (num1 >= num2)
    {
        if (num1 >= num3)
        cout << num1 <<" is the largest number ";
        else
        cout << num3 <<" is the largest number ";
    }
    else
    {
        if (num2 >= num3)
        cout << num2 <<" is the largest number ";
        else
        cout << num3 <<" is the largest number ";
    }
    return 0;
}
Click Here to View the Output!
Enter three numbers:
 -7.8
3.5
4
4 is the largest number
Click Here to View the Explanation!
  • This method doesn’t use the AND operator and instead uses if... else statements within if... else statements. This is known as nested if.
  • For example, in the first part of the code, the compiler checks if num1 is greater than or equal to num2. If yes, it further checks if the statement is greater than num3, otherwise it automatically displays that num3 is the largest number of the three.
  • If not, it checks if num2 is larger than num3. If the statement is marked true, num2 is the largest number is printed on the display screen, else num3 is the largest number is displayed on the screen.