Compare two numbers without using comparison operator

Here given code implementation process.

//  C program 
//  Compare two numbers without using comparison operator
#include <stdio.h>

// Compare two numbers
void isEqual(int x, int y)
{
	// Compare two number
	int result = !(x ^ y);
	// Display given number
	printf("\n Number X : %d", x);
	printf("\n Number Y : %d", y);
	// Display calculated result 
	// 0 indicates false and 1 indicates true
	printf("\n Result : %d", result);
}
int main(int argc, char
	const *argv[])
{
	// Test case
	isEqual(2, 5);
	isEqual(3, 3);
	isEqual(-1, 1);
	return 0;
}

Output

 Number X : 2
 Number Y : 5
 Result : 0
 Number X : 3
 Number Y : 3
 Result : 1
 Number X : -1
 Number Y : 1
 Result : 0
// Include header file
#include <iostream>

using namespace std;
/*
  C++ program
  Compare two numbers without using comparison operator
*/
class Equality
{
	public:
		// Compare two numbers
		void isEqual(int x, int y)
		{
			// Display given number
			cout << "\n Number X : " << x;
			cout << "\n Number Y : " << y;
			// Compare two number
			// 0 indicates false and 1 indicates true
			int result = !(x ^ y);
			cout << "\n Result : " << result;
		}
};
int main()
{
	Equality task = Equality();
	// Test case
	task.isEqual(2, 5);
	task.isEqual(3, 3);
	task.isEqual(-1, 1);
	return 0;
}

Output

 Number X : 2
 Number Y : 5
 Result : 0
 Number X : 3
 Number Y : 3
 Result : 1
 Number X : -1
 Number Y : 1
 Result : 0
<?php
/*
  Php program
  Compare two numbers without using comparison operator
*/
class Equality
{
	// Compare two numbers
	public
	function isEqual($x, $y)
	{
		// Display given number
		echo "\n Number X : ".$x;
		echo "\n Number Y : ".$y;
		// Compare two number
		// 0 indicates false and 1 indicates true
		if ($x ^ $y)
		{
			echo "\n Result : 0";
		}
		else
		{
			echo "\n Result : 1";
		}
	}
}

function main()
{
	$task = new Equality();
	// Test case
	$task->isEqual(2, 5);
	$task->isEqual(3, 3);
	$task->isEqual(-1, 1);
}
main();

Output

 Number X : 2
 Number Y : 5
 Result : 0
 Number X : 3
 Number Y : 3
 Result : 1
 Number X : -1
 Number Y : 1
 Result : 0
/*
  Node Js program
  Compare two numbers without using comparison operator
*/
class Equality
{
	// Compare two numbers
	isEqual(x, y)
	{
		// Display given number
		process.stdout.write("\n Number X : " + x);
		process.stdout.write("\n Number Y : " + y);
		// Compare two number
		var result = !(x ^ y);
		process.stdout.write("\n Result : " + result);
	}
}

function main()
{
	var task = new Equality();
	// Test case
	task.isEqual(2, 5);
	task.isEqual(3, 3);
	task.isEqual(-1, 1);
}
main();

Output

 Number X : 2
 Number Y : 5
 Result : false
 Number X : 3
 Number Y : 3
 Result : true
 Number X : -1
 Number Y : 1
 Result : false
#   Python 3 program
#   Compare two numbers without using comparison operator

class Equality :
	#  Compare two numbers
	def isEqual(self, x, y) :
		#  Display given number
		print("\n Number X : ", x, end = "")
		print("\n Number Y : ", y, end = "")
		#  Compare two number
		result = not(x ^ y)
		print("\n Result : ", result, end = "")
	

def main() :
	task = Equality()
	#  Test case
	task.isEqual(2, 5)
	task.isEqual(3, 3)
	task.isEqual(-1, 1)

if __name__ == "__main__": main()

Output

 Number X :  2
 Number Y :  5
 Result :  False
 Number X :  3
 Number Y :  3
 Result :  True
 Number X :  -1
 Number Y :  1
 Result :  False
#   Ruby program
#   Compare two numbers without using comparison operator

class Equality 
	#  Compare two numbers
	def isEqual(x, y) 
		#  Display given number
		print("\n Number X : ", x)
		print("\n Number Y : ", y)
		#  Compare two number
		#  0 indicates false and 1 indicates true
		if((x ^ y) != 0)
          print("\n Result : ", 0)
        else
          print("\n Result : ", 1)
        end
		
	end

end

def main() 
	task = Equality.new()
	#  Test case
	task.isEqual(2, 5)
	task.isEqual(3, 3)
	task.isEqual(-1, 1)
end

main()

Output

 Number X : 2
 Number Y : 5
 Result : 0
 Number X : 3
 Number Y : 3
 Result : 1
 Number X : -1
 Number Y : 1
 Result : 0
/*
  Scala program
  Compare two numbers without using comparison operator
*/
class Equality
{
	// Compare two numbers
	def isEqual(x: Int, y: Int): Unit = {
		// Display given number
		print("\n Number X : " + x);
		print("\n Number Y : " + y);
		// Compare two number
		// 0 indicates false and 1 indicates true
		if ((x ^ y) != 0)
		{
			print("\n Result : " + 0);
		}
		else
		{
			print("\n Result : " + 1);
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Equality = new Equality();
		// Test case
		task.isEqual(2, 5);
		task.isEqual(3, 3);
		task.isEqual(-1, 1);
	}
}

Output

 Number X : 2
 Number Y : 5
 Result : 0
 Number X : 3
 Number Y : 3
 Result : 1
 Number X : -1
 Number Y : 1
 Result : 0
/*
  Swift 4 program
  Compare two numbers without using comparison operator
*/
class Equality
{
	// Compare two numbers
	func isEqual(_ x: Int, _ y: Int)
	{
		// Display given number
		print("\n Number X : ", x, terminator: "");
		print("\n Number Y : ", y, terminator: "");
		// Compare two number
		// 0 indicates false and 1 indicates true
		if ((x ^ y)  != 0)
		{
			print("\n Result : 0", terminator: "");
		}
		else
		{
			print("\n Result : 1", terminator: "");
		}
	}
}
func main()
{
	let task: Equality = Equality();
	// Test case
	task.isEqual(2, 5);
	task.isEqual(3, 3);
	task.isEqual(-1, 1);
}
main();

Output

 Number X :  2
 Number Y :  5
 Result : 0
 Number X :  3
 Number Y :  3
 Result : 1
 Number X :  -1
 Number Y :  1
 Result : 0
/*
  Kotlin program
  Compare two numbers without using comparison operator
*/
class Equality
{
	// Compare two numbers
	fun isEqual(x: Int, y: Int): Unit
	{
		// Display given number
		print("\n Number X : " + x);
		print("\n Number Y : " + y);
		// Compare two number
		// 0 indicates false and 1 indicates true
		if ((x xor y) != 0)
		{
			print("\n Result : 0");
		}
		else
		{
			print("\n Result : 1");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Equality = Equality();
	// Test case
	task.isEqual(2, 5);
	task.isEqual(3, 3);
	task.isEqual(-1, 1);
}

Output

 Number X : 2
 Number Y : 5
 Result : 0
 Number X : 3
 Number Y : 3
 Result : 1
 Number X : -1
 Number Y : 1
 Result : 0
/*
  Java program
  Compare two numbers without using comparison operator
*/
public class Equality
{
	// Compare two numbers
	public void isEqual(int x, int y)
	{
		// Display given number
		System.out.print("\n Number X : " + x);
		System.out.print("\n Number Y : " + y);
		// Compare two number
		// 0 indicates false and 1 indicates true
		if ((x ^ y) != 0)
		{
			System.out.print("\n Result : 0");
		}
		else
		{
			System.out.print("\n Result : 1");
		}
	}
	public static void main(String[] args)
	{
		Equality task = new Equality();
		// Test case
		task.isEqual(2, 5);
		task.isEqual(3, 3);
		task.isEqual(-1, 1);
	}
}

Output

 Number X : 2
 Number Y : 5
 Result : 0
 Number X : 3
 Number Y : 3
 Result : 1
 Number X : -1
 Number Y : 1
 Result : 0
// Include namespace system
using System;
/*
  C# program
  Compare two numbers without using comparison operator
*/
public class Equality
{
	// Compare two numbers
	public void isEqual(int x, int y)
	{
		// Display given number
		Console.Write("\n Number X : " + x);
		Console.Write("\n Number Y : " + y);
		// Compare two number
		// 0 indicates false and 1 indicates true
		if ((x ^ y) != 0)
		{
			Console.Write("\n Result : 0");
		}
		else
		{
			Console.Write("\n Result : 1");
		}
	}
	public static void Main(String[] args)
	{
		Equality task = new Equality();
		// Test case
		task.isEqual(2, 5);
		task.isEqual(3, 3);
		task.isEqual(-1, 1);
	}
}

Output

 Number X : 2
 Number Y : 5
 Result : 0
 Number X : 3
 Number Y : 3
 Result : 1
 Number X : -1
 Number Y : 1
 Result : 0

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