Algorithm


1. Start
2. Read three numbers (num1, num2, num3) from the user.
3. Initialize a variable max to store the maximum value.
4. Check if num1 is greater than num2 and num1 is greater than num3.
  i. If true, set max to num1.
5. Else, check if num2 is greater than num3.
  i. If true, set max to num2.
6. Else, set max to num3.
7. Print the value of max as the largest number.
8. Stop.

Code Examples

#1 Code Example-C++ Programing Find Largest Number Using if...else Statement

Code - C++ Programming

#include <iostream>
using namespace std;

int main() {
    
    double n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    // check if n1 is the largest number
    if(n1 >= n2 && n1 >= n3)
        cout << "Largest number: " << n1;

    // check if n2 is the largest number
    else if(n2 >= n1 && n2 >= n3)
        cout << "Largest number: " << n2;
    
    // if neither n1 nor n2 are the largest, n3 is the largest
    else 
        cout << "Largest number: " << n3;
  
    return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3

#2 Code Example- C++ Programing Find the Largest Number Using Nested if...else statement

Code - C++ Programming

#include <iostream>
using namespace std;

int main() {

    double n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    // check if n1 is greater than n2
    if (n1 >= n2) {

        // if n1 is also greater than n3,
        // then n1 is the largest number
        if (n1 >= n3)
            cout << "Largest number: " << n1;

        // but if n1 is less than n3
        // but n1 is greater than n2
        // then n3 is the largest number
        else
            cout << "Largest number: " << n3;
    }

     // else, n2 is greater than n1
    else {

        // if n2 is also greater than n3,
        // then n2 is the largest number
        if (n2 >= n3)
            cout << "Largest number: " << n2;

        // but if n2 is less than n3
        // but n2 is greater than n1
        // then n3 is the largest number
        else
            cout << "Largest number: " << n3;
    }

    return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3
Advertisements

Demonstration


C++ Programing Example to Find Largest Number Among Three Numbers-DevsEnv