May be you need something as follows:

m = (a > b) ? ((a > c) ? a : c ) : ((b > c) ? b : c);

15,071,430 members

See more:

Prompt the user to enter three integers from the standard input, read the three integers using scanf(), and display on the standard output the largest number of the

three.

Note:

· User prompt should be “Please enter three integers: ”

· The output format is “The largest value of a, b and c is m” followed by a newline, where a, b and c are the three integers inputted by the user and m is the largest of

them.

My program:

It works with most cases but when i enter 0 -900 1

it gives me 0 as the largest value. why is that?

three.

Note:

· User prompt should be “Please enter three integers: ”

· The output format is “The largest value of a, b and c is m” followed by a newline, where a, b and c are the three integers inputted by the user and m is the largest of

them.

CSS

Copy Code

A sample run of your program is: Please enter three integers: 23 -9 182 The largest value of 23, -9 and 182 is 182 where "23 -9 182" are user input. Note that the second output line ends with a newline so that the cursor is positioned to the beginning of the next line on the screen after the program finishes running.

My program:

It works with most cases but when i enter 0 -900 1

it gives me 0 as the largest value. why is that?

C#

Copy Code

#include <stdio.h> int main() { int a, b, c, m; printf("Please enter three integers: "); scanf("%d %d %d", &a, &b, &c); m = (a>b) ? a : b && (a>c) ? a : c && (b>c) ? b : c; printf("The largest value of %d, %d and %d is %d\n", a, b, c, m); return 0; }

Why not

I mean, if the ternary operator isn't mandatory...

:rolleyes:

Copy Code

if (a > b) { if( a > c ) m = a; else m = c; } else { if (b > c) m = b; else m = c; }

I mean, if the ternary operator isn't mandatory...

:rolleyes:

If you want to use ternary operator there is a trick,

You can use this macro recursively.

Copy Code

#define Max(a,b) ((a>b)?a:b) #include <stdio.h> int main() { int a, b, c, m; printf("Please enter three integers: "); scanf("%d %d %d", &a, &b, &c); m = Max(Max(a,b),c); printf("The largest value of %d, %d and %d is %d\n", a, b, c, m); return 0; }

You can use this macro recursively.

v2

An "accumulating" method :) :

...and its usage:

Copy Code

inline void CheckMax(int& iCurMax, const int& iCheck) { if (iCurMax < iCheck) { iCurMax = iCheck; } }

...and its usage:

Copy Code

```
{
...
int m(MINSHORT);
CheckMax(m, a);
CheckMax(m, b);
CheckMax(m, c);
ASSERT(m >= a &&
m >= b &&
m >= c);
...
}
```

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900