Following are not allowed to use
1) Comparison Operators
2) String functions
Examples:
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++
#include <iostream>
using namespace std;
void areSame( int a, int b)
{
if (a^b)
cout << "Not Same" ;
else
cout << "Same" ;
}
int main()
{
areSame(10, 20);
}
|
Java
class GFG
{
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 );
}
}
|
Python
def areSame(a, b):
if (a ^ b):
print "Not Same"
else :
print "Same"
areSame( 10 , 20 )
|
C#
using System;
class GFG
{
static void areSame( int a, int b)
{
if ( (a ^ b) != 0 )
Console.Write( "Not Same" );
else
Console.Write( "Same" );
}
public static void Main()
{
areSame(10, 20);
}
}
|
PHP
<?php
function areSame( $a , $b )
{
if ( $a ^ $b )
echo "Not Same" ;
else
echo "Same" ;
}
areSame(10, 20);
?>
|
Output:
Not Same
Method 2: We can subtract the numbers. Same numbers yield 0. If answer is not 0, numbers are not same.
C++
#include <bits/stdc++.h>
using namespace std;
void areSame( int a, int b)
{
if (!(a - b))
cout << "Same" ;
else
cout << "Not Same" ;
}
int main()
{
areSame(10, 20);
return 0;
}
|
Java
class GFG{
static void areSame( int a, int b)
{
if ((a - b) == 0 )
System.out.println( "Same" );
else
System.out.println( "Not Same" );
}
public static void main(String args[])
{
areSame( 10 , 20 );
}
}
|
Python
def areSame(a, b):
if ( not (a - b)):
print "Same"
else :
print "Not Same"
areSame( 10 , 20 )
|
C#
using System;
class GFG
{
static void areSame( int a, int b)
{
if ((a - b) == 0)
Console.Write( "Same" );
else
Console.Write( "Not Same" );
}
public static void Main()
{
areSame(10, 20);
}
}
|
PHP
<?php
function areSame( $a , $b )
{
if (!( $a - $b ))
echo "Same" ;
else
echo "Not Same" ;
}
areSame(10, 20);
?>
|
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 contribute.geeksforgeeks.org or mail your article to contribute@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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.