top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

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

+2 votes
97 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
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...