Given two integers A & B. Task is to check if A and B are same or not without using comparison operators.
Examples:
Input : A = 5 , B = 6
Output : 0Input : A = 5 , B = 5
Output : 1Note : 1 = “YES” and 0 = “NO”
The idea is pretty simple we do Xor of both elements ( A, B ). if Xor is zero then two numbers are equal else not.
Below is the implementation of the above idea :
// C++ program to compare two integers without // any comparison operator. #include<bits/stdc++.h> using namespace std;
// function return true if A ^ B > 0 else false bool EqualNumber( int A, int B)
{ return ( A ^ B ) ;
} // Driver program int main()
{ int A = 5 , B = 6;
cout << !EqualNumber(A, B) << endl;
return 0;
} |
// Java program to compare two integers without // any comparison operator. import java.util.*;
class solution
{ // function return true if A ^ B > 0 else false static boolean EqualNumber( int A, int B)
{ if ((A^B) != 0 )
return true ;
else
return false ;
} // Driver program public static void main(String args[])
{ int A = 5 , B = 6 ;
if (EqualNumber(A, B) == false )
System.out.println( 1 );
else System.out.println( 0 );
} } // This code is contributed by // Surendra_Gangwar |
# Python3 program to compare two integers # without any comparison operator. # Function return true if # A ^ B > 0 else false def EqualNumber(A, B):
return ( A ^ B )
# Driver Code A = 5 ; B = 6
print ( int ( not (EqualNumber(A, B))))
# This code is contributed by Smitha Dinesh Semwal. |
// C# program to compare two integers // without any comparison operator. using System;
class GFG
{ // function return true if // A ^ B > 0 else false static bool EqualNumber( int A, int B)
{ if (( A ^ B ) > 0)
return true ;
else
return false ;
} // Driver Code public static void Main()
{ int A = 5 , B = 6;
if (!EqualNumber(A, B) == false )
Console.WriteLine( "0" );
else
Console.WriteLine( "1" );
} } // This code is contributed // by Akanksha Rai |
<?php // PHP program to compare two integers // without any comparison operator. // function return true if // A ^ B > 0 else false function EqualNumber( $A , $B )
{ return ( $A ^ $B ) ;
} // Driver Code $A = 5 ;
$B = 6;
echo ((int)!(EqualNumber( $A , $B ))) . "\n" ;
// This code is contributed // by ChitraNayal ?> |
<script> // JavaScript program to compare two integers without // any comparison operator. // function return true if A ^ B > 0 else false function EqualNumber(A, B)
{ return ( A ^ B ) ;
} // Driver program let A = 5 , B = 6; if (!EqualNumber(A, B) == false )
document.write( "0" );
else document.write( "1" );
// This code is contributed by Surbhi Tyagi. </script> |
0
Time Complexity: O(1)
Auxiliary Space: O(1)
2nd Method:
Another Approach is using “-” (Minus/Subtraction Operator). If the Result of Subtraction between the two Numbers is “Zero”, then they are equal (we return ‘1’), else they are not equal (we return ‘0’).
// C++ program to compare two integers // without any comparison operator. #include <iostream> using namespace std;
// Function return 0 if // A - B != 0 else return 1 int EqualNumber( int A, int B)
{ int diff = A - B;
if (diff)
return 0;
else
return 1;
} // Driver Code int main()
{ int A = 5, B = 6;
cout << EqualNumber(A, B);
return 0;
} // This code is contributed by kothavvsaakash |
import java.util.*;
public class GFG
{ // Function return 0 if
// A - B != 0 else return 1
static int EqualNumber( int A, int B)
{
int diff = A - B;
if (diff != 0 )
return 0 ;
else
return 1 ;
}
// Driver Code
public static void main(String[] args)
{
int A = 5 , B = 6 ;
System.out.println(EqualNumber(A, B));
}
} // This code is contributed by karandeep1234. |
# Python3 program to compare two integers # without any comparison operator. # Function return 0 if # A - B != 0 else return 1 def EqualNumber(A, B):
diff = A - B
if (diff):
return 0
else :
return 1
# Driver Code A = 5 ; B = 6
print ((EqualNumber(A, B)))
# This code is contributed by kothavvsaakash |
// C# program to compare two integers without // any comparison operator. using System;
public class GFG {
// Function return 0 if
// A - B != 0 else return 1
static int EqualNumber( int A, int B)
{
int diff = A - B;
if (diff != 0)
return 0;
else
return 1;
}
static public void Main()
{
// Code
int A = 5, B = 6;
Console.Write(EqualNumber(A, B));
}
} // This code is contributed by lokeshmvs21. |
<script> // JavaScript program to compare two integers without // any comparison operator. function EqualNumber(A, B)
{ var diff = A - B;
if (diff)
return 0
else
return 1
} // Driver program let A = 5 , B = 6; document.write(EqualNumber(A, B)) // This code is contributed by kothavvsaakash. </script> |
0
Time Complexity: O(1)
Auxiliary Space: O(1)