Saturday, May 9, 2009

Solve this problem with use of Ternary Operator in c language.?

write a c program which uses the ternary operator to print -1, 0 or 1 if the value input to it is negative, zero or positive.





ternary operator ----------------------------------------...


first_exp?second_exp : third_exp;





--------------------------------------...


if (first_exp)


x=second_exp;


else


x=third_exp;


--------------------------------------...


i think someone knowing this operator would solve it it seconds.





thanks in advance, i really need it.

Solve this problem with use of Ternary Operator in c language.?
Yes it can be solved using twoTernary operations in a row.





X%26gt;0? X=1:X


X%26lt;0? X=-1:X





If its not greater or less than zero it must be zero. No third test is needed for zero because you have eliminted all other possibilities which would change X if X is anything esle but zero
Reply:ternary operator is a substitute for if..else statement, meaning, there is only a TRUE part and a FALSE part...the problem cannot be solve by ternary alone..
Reply:(x%26lt;0)?(x= -1):((x%26gt;0)?(x=1:x=0)).This is the answer for your question. read it carefully u'll understand.





its simple its nested ternary operator.tat is if x%26lt;0 it sud b -Ve so x= -1 else it will execute the next expression which is another ternary operation tat is if x%26gt;0 it should be greater than 0 that is 1. else its 0. ok.so all 0,1,-1 will be printed accordingly..


No comments:

Post a Comment