Largest no of given 3.

I'm trying to take the largest no of given 3 nos.Currently I can successfully take it from 2 nos.How change the if condition for 3 nos.

#include <iostream>
using namespace std;

int main(){
float x, y;

cout << "X = ";
cin >> x;
cout << "Y = ";
cin >> y;

if (x > y)
{cout << "The Largest is " << x << endl;}
else
{cout << "The Largest is " << y << endl;}

system ("pause");
return 0;
}

closed account (o3hC5Di1)
You will need to add an else if statement:
1
2
3
4
5
6
7
8
9
10
11
12
if (x > y && x > z)
{ 
    //x is the largest 
}
else if (z > x && z > y)
{ 
    // z is the largest 
}
else
{
    //y is the largest
}


Of course you will have to declare z .

All the best,
NwN
Last edited on
Because of the transitivity property max(x,y,z) = max(x, max(y,z))
Topic archived. No new replies allowed.