Single Line C Program for Finding The Elder One Among Three

Remember the Ram, Shayam and Ajay program (from the book Let Us C). This one clears many of my confusions about the if-else ledder. But I din;t satisfied with this one because it;s a lengthy one, not because the program is hard but it takes a little more attention for the nesting of if-else. So I thought to make it short. And tried many ways to achieve what I want. And finally I end up my searching when I learnt about the Conditional Operators ( ?,: ). These operators made me act like OMG :o. These operators actually can do many of coding go simple and I shortened many of my programs with the help of these operators.

Take a Look at the Program ►►

void main()
   int r,s,a;
  printf("Enter the Age of Ram,Shayam and Ajay"); scanf("%d %d %d",&r,&s,&a);  
(r>s?(r>a?printf("Ram is Elder"):printf("Ajay is Elder")):(s>a?printf("Shayam is Elder"):printf("Ajay is Elder")));   getch(); }

And the program made simple. No extra line of code. And also no need for nesting.

You can also take three numbers to find the greater one.

The syntax for Conditional operators is :

if ?(then) :(else)  // write any condition in place of if then following other statements after ?(then) or :(else) …


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s