Check if two numbers are equal without using arithmetic and comparison operators

Following are not allowed to use
1) Arithmetic and Comparison Operators
2) String functions

Method 1 : The idea is to use XOR operator. XOR of two numbers is 0 if the numbers are same, otherwise non-zero.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if two numbers
// are equal without using arithmetic
// and comparison operators
#include <iostream>
using namespace std;
  
// Function to check if two
// numbers are equal using
// XOR operator
void areSame(int a, int b)
{
    if (a ^ b)
        cout << "Not Same";
    else
        cout << "Same";
}
  
// Driver Code
int main()
{
  
    // Calling function
    areSame(10, 20);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if two numbers
// are equal without using arithmetic
// and comparison operators
class GFG {
  
    // Function to check if two
    // numbers are equal using
    // XOR operator
    static void areSame(int a, int b)
    {
        if ((a ^ b) != 0)
            System.out.print("Not Same");
        else
            System.out.print("Same");
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // Calling function
        areSame(10, 20);
    }
}
  
// This code is contributed by Smitha

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check if two numbers
# are equal without using arithmetic
# and comparison operators
  
def areSame(a, b):
  
# Function to check if two
# numbers are equal using 
# XOR operator
 if ((a ^ b) != 0):
    print("Not Same"
 else:
    print("Same")
  
# Driver Code
  
areSame(10, 20)
  
# This code is contributed by Smitha

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if two numbers
// are equal without using arithmetic
// and comparison operators
using System;
  
class GFG {
  
    // Function to check if two
    // numbers are equal using
    // XOR operator
    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(String[] args)
    {
  
        // Calling function
        areSame(10, 20);
    }
}
  
// This code is contributed by Smitha

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if 
// two numbers are equal 
// without using arithmetic 
// and comparison operators
  
// Function to check if two
// numbers are equal using 
// XOR operator
function areSame($a, $b)
{
if ($a ^ $b
echo "Not Same"
else
echo "Same";
}
  
// Driver Code
  
// Calling function
areSame(10, 20);
  
// This code is contributed
// by nitin mittal.
?>

chevron_right



Output :

Not Same

Method 2 : Here idea is using complement ( ~ ) and bit-wise ‘&’ operator.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if two numbers
// are equal without using arithmetic
// and comparison operators
#include <iostream>
using namespace std;
  
// Function to check if two
// numbers are equal using
// using ~ complement and & operator.
void areSame(int a, int b)
{
    if ((a & ~b) == 0 && (~a & b) == 0)
        cout << "Same";
    else
        cout << "Not Same";
}
// Driver Code
int main()
{
  
    // Calling function
    areSame(10, 20);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if two numbers
// are equal without using arithmetic
// and comparison operators
  
class GFG {
    // Function to check if two
    // numbers are equal using
    // using ~ complement and & operator.
    static void areSame(int a, int b)
    {
        if ((a & ~b) == 0 && (~a & b) == 0)
            System.out.print("Same");
        else
            System.out.print("Not Same");
    }
  
    // Driver Code
    public static void main(String args[])
    {
        // Calling function
        areSame(10, 20);
    }
}
  
// This code is contributed
// by Akanksha Rai

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check if two numbers 
# are equal without using arithmetic 
# and comparison operators 
  
# Function to check if two 
# numbers are equal using 
# using ~ complement and & operator. 
def areSame(a, b):
    if ( (a & ~b) == 0 and (~a & b) == 0 ): 
        print("Same"
    else:
        print("Not Same")     
# Calling function 
areSame(10, 20)
  
# This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if two numbers
// are equal without using arithmetic
// and comparison operators
using System;
  
class GFG {
    // Function to check if two
    // numbers are equal using
    // using ~ complement and & operator.
    static void areSame(int a, int b)
    {
        if ((a & ~b) == 0 && (~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 Akanksha Rai

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if two numbers
// are equal without using arithmetic
// and comparison operators
  
// Function to check if two
// numbers are equal using 
// using ~ complement and & operator.
function areSame($a, $b)
{
    if (($a & ~$b)==0 && (~$a & $b)==0) 
        echo "Same"
    else
        echo "Not Same";
}
  
// Driver Code
// Calling function
areSame(1, 1);
  
// This code is contributed by ita_c
?>

chevron_right


Output :



Not Same

Source: https://www.geeksforgeeks.org/count-of-n-digit-numbers-whose-sum-of-digits-equals-to-given-sum/

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.