lundi 27 avril 2020

How to create a function that outputs the largest number using conditions in c++

/*Description: Write a function called getMax that takes three parameters of type int, and returns the biggest of the three parameters which is of type int. */

#include <iostream>
using namespace std;

// Declare the function getMax and put in three variables.


int getMax(int number, int number2, int number3){
    if( number >= number2 && number >= number3){
        cout << number;
        if(number2 >= number && number2 >= number3)
            cout << number2 ;

    }
    else {
        cout << number3;
    }
    return number, number2, number3;
}

// we now use the function to check for largest values below:
int main(){

    cout << getMax(-13, -22, -3) << endl; //Prints -3
    cout << getMax(9, 8, 9) << endl; //prints 9
    cout << getMax(-5, 4, -7) << endl; //prints 4
    cout << getMax(15, 15, 15) << endl; //prints 15
    return 0;

}

Aucun commentaire:

Enregistrer un commentaire