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 ?

### Like this:

Like Loading...

*Related*

tumhare code me galti hai

correct kar lo

-ve waale me

Thanks for pointing out my typing mistake.

I have now corrected it.

Here’s a cheating solution. It has no if statement, and doesn’t evaluate the condition twice. 🙂

#include

int main()

{

int a;

scanf(“%d”,&a);

(a>0)

? printf(“POSITIVE”)

: printf(“NEGATIVE”);

return 1;

}

Yes, It can be the second option until U are told to not to use any type of conditional statement, i.e if switch and ?.

In the post I have included this condition so, I had to do it in this way.

I see. In that case, here’s another one.

No if, switch and ? operator.

There are many other solutions to this.

#include

void main()

{

int a;

scanf(“%d”,&a);

for(;a>0;) { printf(“POSITIVE”); return; }

for(;a<0;) { printf("NEGATIVE"); return; }

}

Yes bro, U can use the way U like.

While solving the problem, our focus remains on the conditions that we must satisfy.

Everyone is free to develop the logic as he wants.

And as U said there are many solutions for this, it is true.

Let’s make this a bit trickier. 🙂

In addition to original requirement of not using conditional keywords like if, switch and ? operator, can you come up with a solution that also do not use any logical operator ( ==, !=, >, =, <= ) ?

So you can't do stuff like (a<0). You may, however, still use logical operator such as ( !, &&, || ).

… Alan

Alan, here is the solution……….

I hope I made it acc to your wish

int main()

{

int a,b,c;

scanf(“%d”,&a);

b=abs(a);

c=a+b;

c&&printf(“POSITIVE”);

(!c)&&printf(“NEGATIVE”);

return 0;

}

…………….CompHacks

Haha. Clever solution, but using abs() is a bit cheating.

If you look at abs() implementation (in Visual Studio),

int __cdecl abs ( int number )

{

return( number>=0 ? number : -number );

}

__int64 __cdecl _abs64(__int64 num)

{

return (num >=0 ? num : -num);

}

Abs() breaks all the requirements :).

What about it ?

int main()

{

int a,b;

scanf(“%d”,&a);

b=pow(2,a);

(!b)&&printf(“NEGATIVE”);

(b)&&printf(“POSITIVE”);

return 0;

}

……………..comphacks

Interesting solution.

pow() is actually a very complicated function, but I will accept your answer. 🙂

Here’s the solution I came up with. I am sure there are many others too.

void main()

{

int a;

scanf(“%d”,&a);

unsigned int signedBit = (a & 0x80000000); // assume int is 32 bit

(!signedBit) && (printf(“POSITIVE”)); // or zero

(signedBit) && (printf(“NEGATIVE”));

}

I am unable to understand that how it would work.

eg. let a=-2

then

b=pow(2,-2)

=2^(-2)

=1/(2^2)

=1/4

=0.25

How it would print -ve ?

The pow function returns 0 when u pass a -ve negative number as its second argument i.e the power.

It is the basic here used. It would return 0 if the number is -ve else it would return a +ve value.

…………………….comphacks

I am still confused over it as it returned .25 for (2,-2).

Look

integer truncates the float value. If U will pass a -ve integer number, Ur answer will always come less than 1, here I am storing this value in integer, so it will turn to 0.

comphacks…. like like like

int a ; if a>0 than +ve, a==0 than zero..but if i want to comapre int a with character than how that possible in c.. means in console if i write, a=’c’ instead of a=45 or a=0 than -ve ans comes… but i want ans like its wrong input.. than how can possible in c