Page 1 of 212> Show 40 post(s) from this thread on one page

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   C puzzle (http://www.go4expert.com/forums/c-puzzle-t19675/)

 vignesh1988i 6Oct2009 09:21

C puzzle

how will you check the relation of two numbers (greater or lesser) without using Relational operators ?????? :D:D:D:nice::nice:

 manishgupta88 6Oct2009 09:25

Re: C puzzle

By comparing them using IF Else

 vignesh1988i 6Oct2009 09:43

Re: C puzzle

for comparing u need relational operator na :):):) , use if-else.. but the question is without relational operators how will u decide the relation between two numbers....? please

 venami 6Oct2009 11:33

Re: C puzzle

You can do it using the following logic:

Let us assume the numbers to be x and y.

Code:

```if(x/y)     printf("x is greater"); else     printf("y is greater");```
Logic:
If you divide a smaller number by a greater number the result will be "zero".

case 1: x<y
(x/y) results "0", in turn makes the condition if(o) => if(false) and so the else part is executed.

case 2:x>y
(x/y) results some positive value, in turn makes the condition to if(true) and so if part is executed.

 vignesh1988i 6Oct2009 18:48

Re: C puzzle

hmm.. it's correct:) good...... but take the worst case , take 'x' and 'y' to be same......... so let us assume x=90 , y=90 , is 90>90 or 90<90 , this statement is false..... so in ur logic , the ur if() will be true for u....... actually it mustt be false.........

and my question is for alll operators , and not only fot > & < , also for >= , <= and ==..... and i ll say a clue , in all relational operators only one thing will change entirely, by changing that u can write the logic and check it......

thank u

 venami 8Oct2009 09:00

Re: C puzzle

Quote:
 Originally Posted by vignesh1988i (Post 58494) and my question is for alll operators , and not only fot > & < , also for >= , <= and ==..... and i ll say a clue , in all relational operators only one thing will change entirely, by changing that u can write the logic and check it......
oh... Can you reveal the solution that you expected?

 vignesh1988i 8Oct2009 23:55

Re: C puzzle

just u try it sir..... it's simple..... :):):):):)

 venami 12Oct2009 16:08

Re: C puzzle

If the two numbers are same, we can find that out using this XOR condition:

Code:

```if(x^y)   printf("Both not equal"); else   printf("Both equal");```
Logic:
If x and y are equal, then condition becomes if(0).

 vignesh1988i 13Oct2009 07:50

Re: C puzzle

HM-MM... for equality this is correct , for lesser or greater wat will be the logic.... :) :) :nice:

 venami 13Oct2009 08:50

Re: C puzzle

I have already posted the result for lesser or greater :D

 All times are GMT +5.5. The time now is 22:45. Page 1 of 212> Show 40 post(s) from this thread on one page