Check if two numbers are equal without using comparison operators - GeeksforGeeks

31 Jan, 2022
Following are not allowed to use 1) Comparison Operators 2) String functionsExamples:  
Input : num1 = 1233, num2 - 1233
Output : Same

Input : num1 = 223, num2 = 233
Output : Not Same
 
Method 1: The idea is to use XOR operator. XOR of two numbers is 0 if the numbers are same, otherwise non-zero.  
C++
Java
Python3
C#
PHP
Javascript
#include <iostream>using namespace std;  // Finds if a and b are same.void areSame(int a, int b){   if (a^b)       cout << "Not Same";   else       cout << "Same";}  int main(){    areSame(10, 20);}
class GFG{    // Finds if a and b are same    static void areSame(int a,int b)    {        if( (a ^ b) != 0 )            System.out.println("Not Same");        else            System.out.println("Same");    }     public static void main(String args[])    {        areSame(10,20);    }     }// This code is contributed by Sumit Ghosh
# Finds if a and b are same.def areSame(a, b):   if (a ^ b):       print("Not Same")   else:       print("Same")   # Driver codeareSame(10, 20) # This code is submitted by Sachin Bisht
// C# program to check if 2// numbers are sameusing System; class GFG{    // Finds if a and b are same    static void areSame(int a, int b)    {        if( (a ^ b) != 0 )          Console.Write("Not Same");        else          Console.Write("Same");    }        // Driver code    public static void Main()    {                        // Calling Function        areSame(10, 20);    }     } // This code is contributed by Nitin Mittal.
<?php // Finds if a and b are same.function areSame($a, $b){    if ($a ^ $b)        echo "Not Same";    else        echo "Same";} // Driver CodeareSame(10, 20); // This code is contributed by nitin mittal?>
<script> // Finds if a and b are same. function areSame(a, b){if (a^b)    document.write("Not Same");else    document.write("Same");} // Driver codeareSame(10, 20); // This code is contributed by Surbhi Tyagi. </script>
Output:  
Not Same
Method 2: We can subtract the numbers. Same numbers yield 0. If answer is not 0, numbers are not same.  
C++
Java
Python3
C#
PHP
Javascript
// CPP code to check if 2 numbers are same#include <bits/stdc++.h>using namespace std; // Finds if a and b are samevoid areSame(int a, int b){    if (!(a - b))        cout << "Same";    else        cout << "Not Same";} // Driver codeint main(){        areSame(10, 20);       return 0;}
// Java code to check if 2 numbers are sameclass GFG{     // Finds if a and b are same    static void areSame(int a, int b)    {        if ((a - b) == 0)            System.out.println("Same");        else            System.out.println("Not Same");    }      // Driver code    public static void main(String args[])    {           areSame(10, 20);            }}//This code is contributed by Sumit Ghosh
# Python code to check if 2 numbers are same # Finds if a and b are samedef areSame(a, b):    if (not(a - b)):        print ("Same")    else:        print ("Not Same")# Driver codeareSame(10, 20) # This code is submitted by Sachin Bisht
// C# code to check if 2// numbers are sameusing System; class GFG{     // Finds if a and b are same    static void areSame(int a, int b)    {        if ((a - b) == 0)            Console.Write("Same");        else            Console.Write("Not Same");    }     // Driver code    public static void Main()    {                 // Calling Function        areSame(10, 20);    }} // This code is contributed by Nitin Mittal.
<?php// PHP code to check if 2// numbers are same // Finds if a and b are samefunction areSame($a, $b){    if (!($a - $b))        echo "Same";    else        echo "Not Same";} // Driver codeareSame(10, 20); // This code is contributed by nitin mittal?>
<script>// javascript code to check if 2 numbers are same   // Finds if a and b are samefunction areSame(a , b){    if ((a - b) == 0)        document.write("Same");    else        document.write("Not Same");    }      // Driver codeareSame(10, 20); // This code contributed by Princi Singh</script>
Output:  
Not Same
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
surbhityagi15
princi singh
amartyaghoshgfg
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Hamming code Implementation in C/C++
Program to find parity
Check whether K-th bit is set or not
Write an Efficient Method to Check if a Number is Multiple of 3
Implementation of Bit Stuffing and Bit Destuffing
Builtin functions of GCC compiler
Swap bits in a given number
Check for Integer Overflow
Bit Tricks for Competitive Programming