top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to compare two numbers in C without using relational operators?

+2 votes
834 views
How to compare two numbers in C without using relational operators?
posted May 28, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

try this code :

#include<bits/stdc++.h>
using namespace std;
int main()
 {
  int num1,num2,diff,absoluteofDiff;
  scanf("%d %d",&num1,&num2);
  diff=num1-num2;
  num1=abs(diff);
  num2=diff+num1;
  if (num1==0)
     printf("num1 == num2\n");
  else if(num2==0)
  printf("num1<num2\n");
  else
  printf("num1>num2\n");
  return 0;
}
answer May 28, 2016 by Shivam Kumar Pandey
+1 vote

Another solution without using '==' too and I used bitwise operator.

#include<stdio.h>
int findMax( int x, int y)
{
   int z = x - y;
   int i  = (z  >>  31)  &  0x1;
   int  max  =  x - i  *  z;
   return max;
}
int main()
{
    int a,b,max;
    scanf("%d %d",&a,&b);
    max=findMax(a,b);
    printf("%d",max);
}
answer May 28, 2016 by Shivam Kumar Pandey
...