Check if two numbers have opposite signs

Here given code implementation process.

// 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


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







© 2021, kalkicode.com, All rights reserved