In the previous article, we have discussed Python Program to Clear nth Bit of a Number
Given two numbers the task is to check whether the given two numbers have opposite signs in Python.
Examples:
Example1:
Input:
Given First Number =3 Given Second Number = -17
Output:
The given two numbers { 3 , -17 } have opposite signsExample2:
Input:
Given First Number =4 Given Second Number = 9
Output:
The given two numbers { 4 , 9 } have same signsProgram to Detect Given Two Integers have Opposite signs in Python
Below are the ways to check whether the given two numbers have opposite signs in Python:
Method #1: Using Xor(^) Operator (Static Input)
Approach:
- Create a function isOppositeSign() which takes the given two numbers as arguments and returns true if they have opposite sign else returns false if they have the same sign.
- Inside the isOppositeSign()Â 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 less than 0 using the if conditional statement.
- If it is true then return True
- Else return False.
- 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 isOppositeSign()Â function and store the result in a variable signResult.
- Check if the Value of SignResult using the If conditional statement.
- If it is true then print the given two numbers have opposite signs.
- Else print the given two numbers have the same sign.
- The Exit of the Program.
Below is the implementation:
# Create a function isOppositeSign()
# which takes the given two numbers as arguments and
# returns true if they have opposite sign
# else returns false if they have the same sign.
def isOppositeSign(first_numb, second_numb):
# Inside the isOppositeSign()Â 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 less than 0
# using the if conditional statement.
if(xor_result < 0):
# If it is true then return True
return True
# Else return False.
return False
# Inside the main code.
# Give the first number as static input and store it in a variable.
firstnumb = 3
# Give the second number as static input and store it in another variable.
secondnumb = -17
# Pass the given two numbers as the arguments to isOppositeSign()Â function
# and store the result in a variable signResult.
signResult = isOppositeSign(firstnumb, secondnumb)
# Check if the Value of SignResult using the If conditional statement.
if(signResult):
# If it is true then print the given two numbers have opposite signs.
print('The given two numbers {', firstnumb,
',', secondnumb, '} have opposite signs')
# Else print the given two numbers have the same sign.
else:
print('The given two numbers {', firstnumb,
',', secondnumb, '} have same signs')
Output:
The given two numbers { 3 , -17 } have opposite signsMethod #2: Using Xor(^) Operator (User Input)
Approach:
- Create a function isOppositeSign() which takes the given two numbers as arguments and returns true if they have opposite sign else returns false if they have the same sign.
- Inside the isOppositeSign()Â 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 less than 0 using the if conditional statement.
- If it is true then return True
- Else return False.
- 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 using the int(input()) function and store it in another variable.
- Pass the given two numbers as the arguments to isOppositeSign()Â function and store the result in a variable signResult.
- Check if the Value of SignResult using the If conditional statement.
- If it is true then print the given two numbers have opposite signs.
- Else print the given two numbers have the same sign.
- The Exit of the Program.
Below is the implementation:
# Create a function isOppositeSign()
# which takes the given two numbers as arguments and
# returns true if they have opposite sign
# else returns false if they have the same sign.
def isOppositeSign(first_numb, second_numb):
# Inside the isOppositeSign()Â 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 less than 0
# using the if conditional statement.
if(xor_result < 0):
# If it is true then return True
return True
# Else return False.
return False
# 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 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 isOppositeSign()Â function
# and store the result in a variable signResult.
signResult = isOppositeSign(firstnumb, secondnumb)
# Check if the Value of SignResult using the If conditional statement.
if(signResult):
# If it is true then print the given two numbers have opposite signs.
print('The given two numbers {', firstnumb,
',', secondnumb, '} have opposite signs')
# Else print the given two numbers have the same sign.
else:
print('The given two numbers {', firstnumb,
',', secondnumb, '} have same signs')
Output:
Enter some random number =4
Enter some random number =9
The given two numbers { 4 , 9 } have same signsGrab the opportunity and utilize the Python Program Code Examples over here to prepare basic and advanced topics too with ease and clear all your doubts.
