Check if two numbers are equal without using comparison operators
Last Updated :
19 Dec, 2022
The following are not allowed to use
- Comparison Operators
- String function
Examples:
Input : num1 = 1233, num2 =1233
Output : Same
Input : num1 = 223, num2 = 233
Output : Not Same
Method 1: The idea is to use the XOR operator. XOR of two numbers is 0 if the numbers are the same, otherwise non-zero.
C++
#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);
}
Java
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
Python3
# Finds if a and b are same.
def areSame(a, b):
if (a ^ b):
print("Not Same")
else:
print("Same")
# Driver code
areSame(10, 20)
# This code is submitted by Sachin Bisht
C#
// C# program to check if 2
// numbers are same
using 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
<?php
// Finds if a and b are same.
function areSame($a, $b)
{
if ($a ^ $b)
echo "Not Same";
else
echo "Same";
}
// Driver Code
areSame(10, 20);
// This code is contributed by nitin mittal
?>
JavaScript
<script>
// Finds if a and b are same.
function areSame(a, b)
{
if (a^b)
document.write("Not Same");
else
document.write("Same");
}
// Driver code
areSame(10, 20);
// This code is contributed by Surbhi Tyagi.
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2: We can subtract the numbers. The same numbers yield 0. If the answer is not 0, the numbers are not the same.
C++
// CPP code to check if 2 numbers are same
#include <bits/stdc++.h>
using namespace std;
// Finds if a and b are same
void areSame(int a, int b)
{
if (!(a - b))
cout << "Same";
else
cout << "Not Same";
}
// Driver code
int main()
{
areSame(10, 20);
return 0;
}
Java
// Java code to check if 2 numbers are same
class 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
Python3
# Python code to check if 2 numbers are same
# Finds if a and b are same
def areSame(a, b):
if (not(a - b)):
print ("Same")
else:
print ("Not Same")
# Driver code
areSame(10, 20)
# This code is submitted by Sachin Bisht
C#
// C# code to check if 2
// numbers are same
using 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
// PHP code to check if 2
// numbers are same
// Finds if a and b are same
function areSame($a, $b)
{
if (!($a - $b))
echo "Same";
else
echo "Not Same";
}
// Driver code
areSame(10, 20);
// This code is contributed by nitin mittal
?>
JavaScript
<script>
// javascript code to check if 2 numbers are same
// Finds if a and b are same
function areSame(a , b)
{
if ((a - b) == 0)
document.write("Same");
else
document.write("Not Same");
}
// Driver code
areSame(10, 20);
// This code contributed by Princi Singh
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 3: The bitwise AND of one number and the complement of the other number is zero if they are the same, and non-zero if they are not the same.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
// Finds if a and b are same.
void areSame(int a, int b)
{
if (a & (~b))
cout << "Not Same" << endl;
else
cout << "Same" << endl;
}
// Driver code
int main()
{
areSame(10, 20);
}
// This code is contributed by phasing17
Java
import java.io.*;
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");
}
// Driver code
public static void main (String[] args) {
areSame(10, 20);
}
}
// This code is contributed by Pushpesh Raj.
Python3
# Python code to check if 2 numbers are same
# Finds if a and b are same
def areSame(a, b):
if (a & ~b):
print ("Not Same")
else:
print ("Same")
# Driver code
areSame(10, 10)
areSame(10, 20)
# This code is contributed by phasing17
C#
// C# program to find min operation to convert a to b
using System;
class GFG
{
// Finds if a and b are same.
static void areSame(int a, int b)
{
if (Convert.ToBoolean(a & (~b)))
Console.WriteLine("Not Same");
else
Console.WriteLine("Same");
}
// Driver code
public static void Main(string[] args)
{
areSame(10, 20);
}
}
// This code is contributed by phasing17
JavaScript
//JavaScript code to implement the approach
// Finds if a and b are same.
function areSame(a, b)
{
if (a & (~b))
console.log("Not Same");
else
console.log("Same");
}
// Driver code
areSame(10, 20);
// This code is contributed by phasing17
Time Complexity: O(1)
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem