Check if two numbers have opposite signs
The problem is to determine whether two given integers have opposite signs or not. Two numbers have opposite signs if one of them is positive and the other is negative or vice versa. In this article, we will explore the concept of opposite signs, provide a suitable example to explain the problem, and then present a pseudocode algorithm to solve it. We will also discuss the resultant output and provide the time complexity of the algorithm.
Example
Let's take the two numbers, A and B, to illustrate the concept of opposite signs:
- A = 5 (positive)
- B = -3 (negative)
In this case, A and B have opposite signs since one is positive, and the other is negative.
Pseudocode
The pseudocode below outlines the steps to determine whether two given integers have opposite signs or not:
1. Start
2. Input A, B // Given integers
3. If (A XOR B) < 0 // XOR is the bitwise exclusive OR operator
4. Print "Opposite signs exist"
5. Else
6. Print "Opposite signs not exist"
7. End
Algorithm Explanation
- Start the algorithm.
- Input the two integers A and B.
- Perform the bitwise XOR (exclusive OR) operation on A and B. XOR returns 1 for each bit position where the two corresponding bits are different; otherwise, it returns 0.
- Check if the result of the XOR operation is less than 0.
- If true, it means the sign bits of A and B are different, indicating that they have opposite signs.
- If false, it means the sign bits of A and B are the same, indicating that they do not have opposite signs.
- Print the appropriate message based on the result of the comparison.
- End the algorithm.
Code Solution
// C Program
// Check if two numbers have opposite signs
#include <stdio.h>
// Determine that signs of two numbers are Opposite or not
void oppositeSigns(int a, int b)
{
// Display given number
printf("\n Number A : %d",a);
printf("\n Number B : %d",b);
if((a ^ b) < 0 == 1 )
{
// When signs is opposite
printf("\n Opposite signs exist \n");
}
else
{
printf("\n Opposite signs not exist\n");
}
}
int main(int argc, char const *argv[])
{
// Test Case
oppositeSigns(1,-1);
oppositeSigns(1,2);
oppositeSigns(-1,2);
return 0;
}
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
/*
Java Program
Check if two numbers have opposite signs
*/
public class Comparison
{
// Determine that signs of two numbers are Opposite or not
public void oppositeSigns(int a, int b)
{
// Display given number
System.out.print("\n Number A : " + a);
System.out.print("\n Number B : " + b);
if ((a ^ b) < 0 == true)
{
// When signs is opposite
System.out.print("\n Opposite signs exist \n");
}
else
{
System.out.print("\n Opposite signs not exist\n");
}
}
public static void main(String[] args)
{
Comparison task = new Comparison();
// Test Case
task.oppositeSigns(1, -1);
task.oppositeSigns(1, 2);
task.oppositeSigns(-1, 2);
}
}
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Check if two numbers have opposite signs
*/
class Comparison
{
public:
// Determine that signs of two numbers are Opposite or not
void oppositeSigns(int a, int b)
{
// Display given number
cout << "\n Number A : " << a;
cout << "\n Number B : " << b;
if ((a ^ b) < 0 == true)
{
// When signs is opposite
cout << "\n Opposite signs exist \n";
}
else
{
cout << "\n Opposite signs not exist\n";
}
}
};
int main()
{
Comparison task = Comparison();
// Test Case
task.oppositeSigns(1, -1);
task.oppositeSigns(1, 2);
task.oppositeSigns(-1, 2);
return 0;
}
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
// Include namespace system
using System;
/*
C# Program
Check if two numbers have opposite signs
*/
public class Comparison
{
// Determine that signs of two numbers are Opposite or not
public void oppositeSigns(int a, int b)
{
// Display given number
Console.Write("\n Number A : " + a);
Console.Write("\n Number B : " + b);
if ((a ^ b) < 0 == true)
{
// When signs is opposite
Console.Write("\n Opposite signs exist \n");
}
else
{
Console.Write("\n Opposite signs not exist\n");
}
}
public static void Main(String[] args)
{
Comparison task = new Comparison();
// Test Case
task.oppositeSigns(1, -1);
task.oppositeSigns(1, 2);
task.oppositeSigns(-1, 2);
}
}
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
<?php
/*
Php Program
Check if two numbers have opposite signs
*/
class Comparison
{
// Determine that signs of two numbers are Opposite or not
public function oppositeSigns($a, $b)
{
// Display given number
echo "\n Number A : ". $a;
echo "\n Number B : ". $b;
if (($a ^ $b) < 0 == true)
{
// When signs is opposite
echo "\n Opposite signs exist \n";
}
else
{
echo "\n Opposite signs not exist\n";
}
}
}
function main()
{
$task = new Comparison();
// Test Case
$task->oppositeSigns(1, -1);
$task->oppositeSigns(1, 2);
$task->oppositeSigns(-1, 2);
}
main();
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
/*
Node Js Program
Check if two numbers have opposite signs
*/
class Comparison
{
// Determine that signs of two numbers are Opposite or not
oppositeSigns(a, b)
{
// Display given number
process.stdout.write("\n Number A : " + a);
process.stdout.write("\n Number B : " + b);
if ((a ^ b) < 0 == true)
{
// When signs is opposite
process.stdout.write("\n Opposite signs exist \n");
}
else
{
process.stdout.write("\n Opposite signs not exist\n");
}
}
}
function main()
{
var task = new Comparison();
// Test Case
task.oppositeSigns(1, -1);
task.oppositeSigns(1, 2);
task.oppositeSigns(-1, 2);
}
main();
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
# Python 3 Program
# Check if two numbers have opposite signs
class Comparison :
# Determine that signs of two numbers are Opposite or not
def oppositeSigns(self, a, b) :
# Display given number
print("\n Number A : ", a, end = "")
print("\n Number B : ", b, end = "")
if ((a ^ b) < 0 == True) :
# When signs is opposite
print("\n Opposite signs exist ")
else :
print("\n Opposite signs not exist")
def main() :
task = Comparison()
# Test Case
task.oppositeSigns(1, -1)
task.oppositeSigns(1, 2)
task.oppositeSigns(-1, 2)
if __name__ == "__main__": main()
Output
Number A : 1
Number B : -1
Opposite signs not exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs not exist
# Ruby Program
# Check if two numbers have opposite signs
class Comparison
# Determine that signs of two numbers are Opposite or not
def oppositeSigns(a, b)
# Display given number
print("\n Number A : ", a)
print("\n Number B : ", b)
if ((a ^ b) < 0 == true)
# When signs is opposite
print("\n Opposite signs exist \n")
else
print("\n Opposite signs not exist\n")
end
end
end
def main()
task = Comparison.new()
# Test Case
task.oppositeSigns(1, -1)
task.oppositeSigns(1, 2)
task.oppositeSigns(-1, 2)
end
main()
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
/*
Scala Program
Check if two numbers have opposite signs
*/
class Comparison
{
// Determine that signs of two numbers are Opposite or not
def oppositeSigns(a: Int, b: Int): Unit = {
// Display given number
print("\n Number A : " + a);
print("\n Number B : " + b);
if ((a ^ b) < 0 == true)
{
// When signs is opposite
print("\n Opposite signs exist \n");
}
else
{
print("\n Opposite signs not exist\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Comparison = new Comparison();
// Test Case
task.oppositeSigns(1, -1);
task.oppositeSigns(1, 2);
task.oppositeSigns(-1, 2);
}
}
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
/*
Swift 4 Program
Check if two numbers have opposite signs
*/
class Comparison
{
// Determine that signs of two numbers are Opposite or not
func oppositeSigns(_ a: Int, _ b: Int)
{
// Display given number
print("\n Number A : ", a, terminator: "");
print("\n Number B : ", b, terminator: "");
if (((a ^ b) < 0) == true)
{
// When signs is opposite
print("\n Opposite signs exist ");
}
else
{
print("\n Opposite signs not exist");
}
}
}
func main()
{
let task: Comparison = Comparison();
// Test Case
task.oppositeSigns(1, -1);
task.oppositeSigns(1, 2);
task.oppositeSigns(-1, 2);
}
main();
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
/*
Kotlin Program
Check if two numbers have opposite signs
*/
class Comparison
{
// Determine that signs of two numbers are Opposite or not
fun oppositeSigns(a: Int, b: Int): Unit
{
// Display given number
print("\n Number A : " + a);
print("\n Number B : " + b);
if ((a xor b) < 0 == true)
{
// When signs is opposite
print("\n Opposite signs exist \n");
}
else
{
print("\n Opposite signs not exist\n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Comparison = Comparison();
// Test Case
task.oppositeSigns(1, -1);
task.oppositeSigns(1, 2);
task.oppositeSigns(-1, 2);
}
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
/*
Rust Program
Check if two numbers have opposite signs
*/
fn main()
{
// Test Case
opposite_signs(1, -1);
opposite_signs(1, 2);
opposite_signs(-1, 2);
}
fn opposite_signs(a: i32, b: i32)
{
// Display given number
print!("\n Number A : {}", a);
print!("\n Number B : {}", b);
if ((a ^ b) < 0) == true
{
// When signs is opposite
print!("\n Opposite signs exist \n");
}
else
{
print!("\n Opposite signs not exist\n");
}
}
Output
Number A : 1
Number B : -1
Opposite signs exist
Number A : 1
Number B : 2
Opposite signs not exist
Number A : -1
Number B : 2
Opposite signs exist
Output Explanation
Let's apply the given test cases to the algorithm:
-
oppositeSigns(1, -1);
- Number A: 1 (positive)
- Number B: -1 (negative)
- XOR(1, -1) = 1 ^ -1 = -2 (negative)
- The result of the XOR operation is less than 0, so the function prints "Opposite signs exist."
-
oppositeSigns(1, 2);
- Number A: 1 (positive)
- Number B: 2 (positive)
- XOR(1, 2) = 1 ^ 2 = 3 (positive)
- The result of the XOR operation is not less than 0, so the function prints "Opposite signs not exist."
-
oppositeSigns(-1, 2);
- Number A: -1 (negative)
- Number B: 2 (positive)
- XOR(-1, 2) = -1 ^ 2 = -3 (negative)
- The result of the XOR operation is less than 0, so the function prints "Opposite signs exist."
Time Complexity
The time complexity of the algorithm is O(1) because the operations performed (input, XOR, and comparison) are constant-time operations and do not depend on the size of the input. Therefore, the algorithm's performance remains constant regardless of the input size.
The given problem is about determining whether two given integers have opposite signs or not. By using the XOR operation on the two numbers, we can efficiently check their signs and provide an appropriate output. The algorithm's time complexity is constant, making it efficient for practical use.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment