Write a program in python to store two numbers. Find which number is greater. (Include "or"/"and"/"not"&comparison operators in the solution)
Answers
Explanation:
The idea is to use XOR operator. XOR of two numbers is 0 if the numbers are same, otherwise non-zero.
C++
// 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);
}
JavaPython 3C#PHP
Output :Not Same
Method 2 : Here idea is using complement ( ~ ) and bit-wise ‘&’ operator.
C++
// 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()
{