I have two integer numbers. I want to find which number is greater than other one without using relational operators.

Hello...
This is Neigyl...
I'm still a newbie here...
I hope this'll help...

#include <stdio.h>
#include <stdlib.h>

int main ()
{
   int a = 11, b = 10;

   /* We didn't use a relational operator here but the definition
       of max() and min() in stdlib.h uses so. */

   printf ("The greater value is %d", max(a, b));
   printf ("\nThe lesser value is %d", min(a, b));

   /* We didn't use a relational operator here in a sense that such
       operator didn't appear inside the if. */

   if (a - b + abs(a - b))
      printf ("a is greater than b.");
   else
      printf ("a is lesser or equal than b.");

   a = b = 5;

   if (a - b)
      printf ("They are not equal.");
   else
      printf ("They are equal.");

   return 0;
   /* With the 2 scenarios above, we never used the relational operator in our program. */
}

It is often prudent to test around the edges.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int cmp1(int a, int b)
{
   return a == b ? 0 : a < b ? -1 : 1;
}

int cmp2(int x, int y)
{
   if ( x - y + abs(x-y) )
   {
      return 1;
   }
   if ( y - x + abs(y-x) )
   {
      return -1;
   }
   return 0;
}

int main(void)
{
   int val[] = { 42, -5, 0, 9, INT_MIN, INT_MAX, INT_MIN + 1, INT_MAX - 1 };
   size_t i, j;
   for ( i = 0; i < sizeof val / sizeof *val; ++i )
   {
      for ( j = 0; j < sizeof val / sizeof *val; ++j )
      {
         printf("[%11d,%11d] : %2d | %2d\n", val[i], val[j], 
                cmp1(val[i], val[j]), cmp2(val[i], val[j]));
      }
   }
   return 0;
}

Let's take a look at places where the output differs.

/* my output
[         42,         42] :  0 |  0
[         42,         -5] :  1 |  1
[         42,          0] :  1 |  1
[         42,          9] :  1 |  1
[         42,-2147483648] :  1 | -1
[         42, 2147483647] : -1 | -1
[         42,-2147483647] :  1 | -1
[         42, 2147483646] : -1 | -1
[         -5,         42] : -1 | -1
[         -5,         -5] :  0 |  0
[         -5,          0] : -1 | -1
[         -5,          9] : -1 | -1
[         -5,-2147483648] :  1 |  1
[         -5, 2147483647] : -1 |  1
[         -5,-2147483647] :  1 |  1
[         -5, 2147483646] : -1 |  1
[          0,         42] : -1 | -1
[          0,         -5] :  1 |  1
[          0,          0] :  0 |  0
[          0,          9] : -1 | -1
[          0,-2147483648] :  1 |  0
[          0, 2147483647] : -1 | -1
[          0,-2147483647] :  1 |  1
[          0, 2147483646] : -1 | -1
[          9,         42] : -1 | -1
[          9,         -5] :  1 |  1
[          9,          0] :  1 |  1
[          9,          9] :  0 |  0
[          9,-2147483648] :  1 | -1
[          9, 2147483647] : -1 | -1
[          9,-2147483647] :  1 | -1
[          9, 2147483646] : -1 | -1
[-2147483648,         42] : -1 |  1
[-2147483648,         -5] : -1 | -1
[-2147483648,          0] : -1 |  0
[-2147483648,          9] : -1 |  1
[-2147483648,-2147483648] :  0 |  0
[-2147483648, 2147483647] : -1 |  1
[-2147483648,-2147483647] : -1 | -1
[-2147483648, 2147483646] : -1 |  1
[ 2147483647,         42] :  1 |  1
[ 2147483647,         -5] :  1 | -1
[ 2147483647,          0] :  1 |  1
[ 2147483647,          9] :  1 |  1
[ 2147483647,-2147483648] :  1 | -1
[ 2147483647, 2147483647] :  0 |  0
[ 2147483647,-2147483647] :  1 | -1
[ 2147483647, 2147483646] :  1 |  1
[-2147483647,         42] : -1 |  1
[-2147483647,         -5] : -1 | -1
[-2147483647,          0] : -1 | -1
[-2147483647,          9] : -1 |  1
[-2147483647,-2147483648] :  1 |  1
[-2147483647, 2147483647] : -1 |  1
[-2147483647,-2147483647] :  0 |  0
[-2147483647, 2147483646] : -1 |  1
[ 2147483646,         42] :  1 |  1
[ 2147483646,         -5] :  1 | -1
[ 2147483646,          0] :  1 |  1
[ 2147483646,          9] :  1 |  1
[ 2147483646,-2147483648] :  1 | -1
[ 2147483646, 2147483647] : -1 | -1
[ 2147483646,-2147483647] :  1 | -1
[ 2147483646, 2147483646] :  0 |  0
*/
Comments
Yes indeed
This question has already been answered. Start a new discussion instead.