Hi guys is this possible to write a biggest number program( biggest among 3) in a single line

4
Contributors
5
Replies
7
Views
8 Years
Discussion Span
Last Post by 0x69

You can try this for three numbers a, b and c :

``printf("\nLargest among the three is %d\n", ((((a>b)?a:b)>c)?((a>b)?a:b):c));``

But there are other ways too....try to find out !!!

I am a fresher mitra pls tell me the other ways

This is the simplest way i thought it could be done...
Its a simple ternary operator (you can consider as "if else" blocks)

I will explain it with an easy example :
Suppose if a=2, b=1 and c=3.
Now our expression is ((a>b)?a:b)>c)?((a>b)?a:b):c)):
Consider (a>b)?a:b expression :
(a>b) is true so (a>b)?a:b will mean "a"... right !!!
now by replacing that part(highlighted in red below) in the expression by "a",
((a>b)?a:b)>c)?((a>b)?a:b):c))
we will get:

(a>c)?a:c
which means "c", as (a>c) is false.

So, c is the largest number and will be printed as the output.
Isn't it simple ???? ;)

ganesh read "let us c" book thoroughly.... amazing book on c programming

And a bit shorter version ;)

``((a>b&&a>c)?a:((b>c)?b:c))``
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.