{"id":20471,"date":"2021-09-13T14:51:16","date_gmt":"2021-09-13T09:21:16","guid":{"rendered":"https:\/\/python-programs.com\/?p=20471"},"modified":"2021-11-22T18:36:20","modified_gmt":"2021-11-22T13:06:20","slug":"python-program-to-check-given-two-integers-have-opposite-signs","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-given-two-integers-have-opposite-signs\/","title":{"rendered":"Python Program to Check Given Two Integers have Opposite signs"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Clear nth Bit of a Number<\/a><\/p>\n Given two numbers the task is to check whether the given two numbers have opposite signs in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check whether the given two numbers have opposite signs in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Grab the opportunity and utilize the Python Program Code Examples<\/a> over here to prepare basic and advanced topics too with ease and clear all your doubts.<\/p>\n 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 …<\/p>\nGiven First Number =3\r\nGiven Second Number = -17<\/pre>\n
The given two numbers { 3 , -17 } have opposite signs<\/pre>\n
Given First Number =4\r\nGiven Second Number = 9<\/pre>\n
The given two numbers { 4 , 9 } have same signs<\/pre>\n
Program to Detect Given Two Integers have Opposite signs in Python<\/h2>\n
\n
Method #1: Using Xor(^) Operator (Static Input)<\/h3>\n
\n
# Create a function isOppositeSign()\r\n# which takes the given two numbers as arguments and\r\n# returns true if they have opposite sign\r\n# else returns false if they have the same sign.\r\n\r\n\r\ndef isOppositeSign(first_numb, second_numb):\r\n # Inside the isOppositeSign()\u00a0function.\r\n # Apply xor to the first number and second number and\r\n # store it in a variable say xor_result.\r\n xor_result = first_numb ^ second_numb\r\n # Check if the value of xor_result is less than 0\r\n # using the if conditional statement.\r\n if(xor_result < 0):\r\n # If it is true then return True\r\n return True\r\n # Else return False.\r\n return False\r\n\r\n\r\n# Inside the main code.\r\n# Give the first number as static input and store it in a variable.\r\nfirstnumb = 3\r\n# Give the second number as static input and store it in another variable.\r\nsecondnumb = -17\r\n# Pass the given two numbers as the arguments to isOppositeSign()\u00a0function\r\n# and store the result in a variable signResult.\r\nsignResult = isOppositeSign(firstnumb, secondnumb)\r\n# Check if the Value of SignResult using the If conditional statement.\r\nif(signResult):\r\n # If it is true then print the given two numbers have opposite signs.\r\n print('The given two numbers {', firstnumb,\r\n ',', secondnumb, '} have opposite signs')\r\n# Else print the given two numbers have the same sign.\r\nelse:\r\n print('The given two numbers {', firstnumb,\r\n ',', secondnumb, '} have same signs')\r\n<\/pre>\n
The given two numbers { 3 , -17 } have opposite signs<\/pre>\n
Method #2: Using Xor(^) Operator (User Input)<\/h3>\n
\n
# Create a function isOppositeSign()\r\n# which takes the given two numbers as arguments and\r\n# returns true if they have opposite sign\r\n# else returns false if they have the same sign.\r\n\r\n\r\ndef isOppositeSign(first_numb, second_numb):\r\n # Inside the isOppositeSign()\u00a0function.\r\n # Apply xor to the first number and second number and\r\n # store it in a variable say xor_result.\r\n xor_result = first_numb ^ second_numb\r\n # Check if the value of xor_result is less than 0\r\n # using the if conditional statement.\r\n if(xor_result < 0):\r\n # If it is true then return True\r\n return True\r\n # Else return False.\r\n return False\r\n\r\n\r\n# Inside the main code.\r\n# Give the first number as user input using the int(input()) function\r\n# and store it in a variable.\r\nfirstnumb = int(input('Enter some random number ='))\r\n# Give the second number using the int(input()) function and store it in another variable.\r\nsecondnumb = int(input('Enter some random number ='))\r\n# Pass the given two numbers as the arguments to isOppositeSign()\u00a0function\r\n# and store the result in a variable signResult.\r\nsignResult = isOppositeSign(firstnumb, secondnumb)\r\n# Check if the Value of SignResult using the If conditional statement.\r\nif(signResult):\r\n # If it is true then print the given two numbers have opposite signs.\r\n print('The given two numbers {', firstnumb,\r\n ',', secondnumb, '} have opposite signs')\r\n# Else print the given two numbers have the same sign.\r\nelse:\r\n print('The given two numbers {', firstnumb,\r\n ',', secondnumb, '} have same signs')\r\n<\/pre>\n
Enter some random number =4\r\nEnter some random number =9\r\nThe given two numbers { 4 , 9 } have same signs<\/pre>\n
\n