English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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;

-----------------------------------------------means----------------
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.

2007-03-16 20:46:10 · 3 answers · asked by Amar G 1 in Computers & Internet Programming & Design

3 answers

Yes it can be solved using twoTernary operations in a row.

X>0? X=1:X
X<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

2007-03-16 23:28:36 · answer #1 · answered by MarkG 7 · 0 0

(x<0)?(x= -1):((x>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<0 it sud b -Ve so x= -1 else it will execute the next expression which is another ternary operation tat is if x>0 it should be greater than 0 that is 1. else its 0. ok.so all 0,1,-1 will be printed accordingly..

2007-03-17 08:12:38 · answer #2 · answered by sm030 2 · 0 0

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..

2007-03-17 04:53:28 · answer #3 · answered by vitualpalab0i 1 · 0 1

fedest.com, questions and answers