How to make on C++ to show out the bigger number

Hi, I'm new here and I have one little problem. We have to make a program which need to show out the bigger number (from three numbers). I can make this with function if(), but we need to make without if().
This is for two number:
#include<iostream.h>
#include<math.h>
int main()
{
double y,x0,x1;
cout<<"insert two different numbers"<<endl;
cin>>x0;
cin>>x1;

y=0.5*(x0+x1+fabs(x0-x1));
cout<<"max= "<<y<<endl;
return 0;
}
but when I try with three a something make wrong. Thank you in advance



Last edited on

cout << "max = " << ( y > x0 ? ( y>x1?y:x1) : (x1>x0?x1:x0));
Thanks man, I make some differents, but at the end, the program work. Thanks again.
if you were wandering the ternary operator ?: is pretty similar to an if statement. the syntax is as follows:

expression ? [value if true] :[value if false]

so the above code is very similar to:

1
2
3
4
5
6
7
8
9
10
11
        if ( y > x0){
              if (y >x1)
                  cout << y;
              else
                  cout << x1;
              
              if (x1 > x0)
                  cout << x1;
              else
                  cout << x0;
          }


However I prefer the above.
Last edited on
Topic archived. No new replies allowed.