C++ Program to find largest of 3 numbers

0
2377
Drawing a Triangle in Console window
Converting lowercase to uppercase in C++

In C++ we can implement a logic to write a program to find largest of 3 numbers. So for finding maximum number from given two, three or more numbers, this is just simple, we just need to use the ” if ” conditional statement and use the ” < ” or ” > ” operators to check out the maximum number or minimum number or whatever it is. This is the C++ program to find largest of 3 numbers given:

    // program to find largest of 3 numbers entered by user.
    #include <iostream>
    using namespace std;
    double maximum( double, double, double ); // function prototype
     
    int main()
    {
        double number1;
        double number2;
        double number3;
     
        cout << "Enter three floating-point numbers: ";
        cin >> number1 >> number2 >> number3;
     
        // number1, number2 and number3 are arguments to
        // the maximum function call
        cout << "Maximum is: "
            << maximum( number1, number2, number3 ) << endl;
        return 0;  // indicates successful termination
    }
     
    double maximum( double x, double y, double z )
    {
        double max = x;
        if ( y > max )    // if y is larger,
            max = y;
        if ( z > max )    // if z is larger,
            max = z;
    return max;       // max is largest value
     
    } // end function maximum

In the above given code as explained in comments, we are given three numbers of datatype “double” in which we are going to find out the maximum number .Then we passed it to the function which is able to decide the maximum number, in the function we used the ” if ” conditional statement and found the largest of 3 numbers and then returned it to the main().

 

Drawing a Triangle in Console window
Converting lowercase to uppercase in C++

Leave a Reply