I found a tricky question somewhere that asked to cheak whether a given number is greater than 0 without using any type of conditional keywords like if, switch and ? operator. Now question involves usage of a concept of operators that U almost all know. Let me solve it for U.Have U heard about && operator. Yes It is And Operator. It produces 1 when both of its operand are 1 or being more specific TRUE. As I told U in my earlier posts about the return value of printf() statemnt, it return 1 on sucessful printing which is always true.

Let Us Use the && operator to solve this problem. Now We have one of its operator as printf() that always returns 1.

Therefore, we need one more statement that produces 1 so that our && operator could work properly. Can U guess it ?

Let me tell the statement it will be (num>0), LOOK if the number is greater than 0 then the statement will be true and would return 1 and our && operator would work.

So, here is the final code

int main()

{

int a;

scanf(“%d”,&a);

(a>0)&&(printf(“POSITIVE”));

(a<0)&&(printf(“NEGATIVE”));

return 1;

}

So, Did U got the answer or Not ?

Advertisements