Python compare two numbers – Python Program to Check if Two Numbers are Equal Without using Arithmetic and Comparison Operators

Python compare two numbers: In the previous article, we have discussed Python Program to Check Given Two Integers have Opposite signs

Given two numbers the task is to check whether the given two numbers are equal in Python.

Examples:

Example1:

Input:

Given First Number = 10
Given Second Number = 10

Output:

The given two numbers { 10 , 10 } are Equal

Example2:

Input:

Given First Number = 8
Given Second Number = 15

Output:

The given two numbers { 8 , 15 } are Not equal

Program to Check if Two Numbers are Equal Without using Arithmetic and Comparison Operators in Python

Below are the ways to check whether the given two numbers are equal in Python:

Method #1: Using Xor(^) Operator (Static Input)

Approach:

  • Create a function isEqualNumbers() which takes the given two numbers as arguments and returns true if they are equal else returns false if they are not equal.
  • Inside the isEqualNumbers() function.
  • Apply xor to the first number and second number and store it in a variable say xor_result.
  • Check if the value of xor_result is not equal to 0 using the if conditional statement.
  • If it is true then return False
  • Else return True.
  • Inside the main code.
  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the given two numbers as the arguments to isEqualNumbers() function and store the result in a variable Reslt.
  • Check if the Value of Reslt using the If conditional statement.
  • If it is true then print the given two numbers are equal.
  • Else print the given two numbers are not equal.
  • The Exit of the Program.

Below is the implementation:

# Create a function isEqualNumbers()
# which takes the given two numbers as arguments and
# returns true if they are equal
# else returns false if they are not equal.


def isEqualNumbers(first_numb, second_numb):
        # Inside the isEqualNumbers() function.
        # Apply xor to the first number and second number and
    # store it in a variable say xor_result.
    xor_result = first_numb ^ second_numb
    # Check if the value of xor_result is not equal to 0
    # using the if conditional statement.
    if(xor_result != 0):
        # If it is true then return False
        return False
    # Else return True.
    return True


# Inside the main code.
# Give the first number as static input and store it in a variable.
firstnumb = 10
# Give the second number as static input and store it in another variable.
secondnumb = 10
# Pass the given two numbers as the arguments to isEqualNumbers() function
# and store the result in a variable Reslt.
Reslt = isEqualNumbers(firstnumb, secondnumb)
# Check if the Value of Reslt using the If conditional statement.
if(Reslt):
        # If it is true then print the given two numbers are Equal.
    print('The given two numbers {', firstnumb,
          ',', secondnumb, '} are Equal')
# Else print the given two numbers are Not Equal.
else:
    print('The given two numbers {', firstnumb,
          ',', secondnumb, '} are Not equal')

Output:

The given two numbers { 10 , 10 } are Equal

Method #2: Using Xor(^) Operator (User Input)

Approach:

  • Create a function isEqualNumbers() which takes the given two numbers as arguments and returns true if they are equal else returns false if they are not equal.
  • Inside the isEqualNumbers() function.
  • Apply xor to the first number and second number and store it in a variable say xor_result.
  • Check if the value of xor_result is not equal to 0 using the if conditional statement.
  • If it is true then return False
  • Else return True.
  • Inside the main code.
  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the given two numbers as the arguments to isEqualNumbers() function and store the result in a variable Reslt.
  • Check if the Value of Reslt using the If conditional statement.
  • If it is true then print the given two numbers are equal.
  • Else print the given two numbers are not equal.
  • The Exit of the Program.

Below is the implementation:

# Create a function isEqualNumbers()
# which takes the given two numbers as arguments and
# returns true if they are equal
# else returns false if they are not equal.


def isEqualNumbers(first_numb, second_numb):
        # Inside the isEqualNumbers() function.
        # Apply xor to the first number and second number and
    # store it in a variable say xor_result.
    xor_result = first_numb ^ second_numb
    # Check if the value of xor_result is not equal to 0
    # using the if conditional statement.
    if(xor_result != 0):
        # If it is true then return False
        return False
    # Else return True.
    return True


# Inside the main code.
# Give the first number as user input using the int(input()) function and store it in a variable.
firstnumb = int(input('Enter some random number ='))
# Give the second number as user input using the int(input()) function and 
# store it in another variable.
secondnumb = int(input('Enter some random number ='))
# Pass the given two numbers as the arguments to isEqualNumbers() function
# and store the result in a variable Reslt.
Reslt = isEqualNumbers(firstnumb, secondnumb)
# Check if the Value of Reslt using the If conditional statement.
if(Reslt):
        # If it is true then print the given two numbers are Equal.
    print('The given two numbers {', firstnumb,
          ',', secondnumb, '} are Equal')
# Else print the given two numbers are Not Equal.
else:
    print('The given two numbers {', firstnumb,
          ',', secondnumb, '} are Not equal')

Output:

Enter some random number =8
Enter some random number =15
The given two numbers { 8 , 15 } are Not equal

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.