Find largest number without using relational operator

Here given code implementation process.

// C program for 
// Find largest number without using relational operator
#include <stdio.h>
#include <stdlib.h>

void largest(int a, int b)
{
   // Find largest
   int result = (abs(a - b) + (a + b)) / 2;
   // Display given numbers
   printf(" Given number a : %d  b : %d \n",a,b);
   // Display the calculated largest number
   printf(" Largest : %d\n", result);
}

int main(int argc, char const *argv[])
{
    // Test cases
    largest(23,2);
    largest(-5,-3);
    largest(52,234);
    largest(-1,-4);

    return 0;
}

input

 Given number a : 23  b : 2
 Largest : 23
 Given number a : -5  b : -3
 Largest : -3
 Given number a : 52  b : 234
 Largest : 234
 Given number a : -1  b : -4
 Largest : -1
/*
  Java Program for 
  Find largest number without using relational operator
*/
public class FindLargest
{
    public void largest(int a, int b)
    {
        // Find largest
        int result = (Math.abs(a - b) + (a + b)) / 2;
        // Display given numbers
        System.out.println(" Given number a : " + a + " b : " + b );
        // Display the calculated largest number
        System.out.println(" Largest : " + result );
    }
    public static void main(String[] args)
    {
        FindLargest task = new FindLargest();
        // Test Cases
        task.largest(23,2);
        task.largest(-5,-3);
        task.largest(52,234);
        task.largest(-1,-4);

    }
}

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
// Include header file
#include <iostream>
#include <math.h>

using namespace std;
/*
  C++ Program for 
  Find largest number without using relational operator
*/
class FindLargest
{
	public: void largest(int a, int b)
	{
		// Find largest
		int result = (abs(a - b) + (a + b)) / 2;
		// Display given numbers
		cout << " Given number a : " << a << " b : " << b << endl;
		// Display the calculated largest number
		cout << " Largest : " << result << endl;
	}
};
int main()
{
	FindLargest *task = new FindLargest();
	// Test Cases
	task->largest(23, 2);
	task->largest(-5, -3);
	task->largest(52, 234);
	task->largest(-1, -4);
	return 0;
}

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
// Include namespace system
using System;
/*
  Csharp Program for 
  Find largest number without using relational operator
*/
public class FindLargest
{
	public void largest(int a, int b)
	{
		// Find largest
		int result = (Math.Abs(a - b) + (a + b)) / 2;
		// Display given numbers
		Console.WriteLine(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		Console.WriteLine(" Largest : " + result);
	}
	public static void Main(String[] args)
	{
		FindLargest task = new FindLargest();
		// Test Cases
		task.largest(23, 2);
		task.largest(-5, -3);
		task.largest(52, 234);
		task.largest(-1, -4);
	}
}

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
<?php
/*
  Php Program for 
  Find largest number without using relational operator
*/
class FindLargest
{
	public	function largest($a, $b)
	{
		// Find largest
		$result = (int)((abs($a - $b) + ($a + $b)) / 2);
		// Display given numbers
		echo " Given number a : ".$a.
		" b : ".$b.
		"\n";
		// Display the calculated largest number
		echo " Largest : ".$result.
		"\n";
	}
}

function main()
{
	$task = new FindLargest();
	// Test Cases
	$task->largest(23, 2);
	$task->largest(-5, -3);
	$task->largest(52, 234);
	$task->largest(-1, -4);
}
main();

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
/*
  Node JS Program for 
  Find largest number without using relational operator
*/
class FindLargest
{
	largest(a, b)
	{
		// Find largest
		var result = parseInt((Math.abs(a - b) + (a + b)) / 2);
		// Display given numbers
		console.log(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		console.log(" Largest : " + result);
	}
}

function main()
{
	var task = new FindLargest();
	// Test Cases
	task.largest(23, 2);
	task.largest(-5, -3);
	task.largest(52, 234);
	task.largest(-1, -4);
}
main();

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
import math
#  Python 3 Program for 
#  Find largest number without using relational operator
class FindLargest :
	def largest(self, a, b) :
		result = int((abs(a - b) + (a + b)) / 2)
		#  Display given numbers
		print(" Given number a : ", a ," b : ", b)
		#  Display the calculated largest number
		print(" Largest : ", result)
	

def main() :
	task = FindLargest()
	#  Test Cases
	task.largest(23, 2)
	task.largest(-5, -3)
	task.largest(52, 234)
	task.largest(-1, -4)

if __name__ == "__main__": main()

input

 Given number a :  23  b :  2
 Largest :  23
 Given number a :  -5  b :  -3
 Largest :  -3
 Given number a :  52  b :  234
 Largest :  234
 Given number a :  -1  b :  -4
 Largest :  -1
#  Ruby Program for 
#  Find largest number without using relational operator
class FindLargest 
	def largest(a, b) 
		#  Find largest
		result = ((a - b).abs + (a + b)) / 2
		#  Display given numbers
		print(" Given number a : ", a ," b : ", b, "\n")
		#  Display the calculated largest number
		print(" Largest : ", result, "\n")
	end

end

def main() 
	task = FindLargest.new()
	#  Test Cases
	task.largest(23, 2)
	task.largest(-5, -3)
	task.largest(52, 234)
	task.largest(-1, -4)
end

main()

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
/*
  Scala Program for 
  Find largest number without using relational operator
*/
class FindLargest()
{
	def largest(a: Int, b: Int): Unit = {
		// Find largest
		var result: Int = ((Math.abs(a - b) + (a + b)) / 2).toInt;
		// Display given numbers
		println(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		println(" Largest : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: FindLargest = new FindLargest();
		// Test Cases
		task.largest(23, 2);
		task.largest(-5, -3);
		task.largest(52, 234);
		task.largest(-1, -4);
	}
}

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
/*
  Swift 4 Program for 
  Find largest number without using relational operator
*/
class FindLargest
{
	func largest(_ a: Int, _ b: Int)
	{
		// Find largest
		let result: Int = (abs(a - b) + (a + b)) / 2;
		// Display given numbers
		print(" Given number a : ", a ," b : ", b);
		// Display the calculated largest number
		print(" Largest : ", result);
	}
}
func main()
{
	let task: FindLargest = FindLargest();
	// Test Cases
	task.largest(23, 2);
	task.largest(-5, -3);
	task.largest(52, 234);
	task.largest(-1, -4);
}
main();

input

 Given number a :  23  b :  2
 Largest :  23
 Given number a :  -5  b :  -3
 Largest :  -3
 Given number a :  52  b :  234
 Largest :  234
 Given number a :  -1  b :  -4
 Largest :  -1
/*
  Kotlin Program for 
  Find largest number without using relational operator
*/
class FindLargest
{
	fun largest(a: Int, b: Int): Unit
	{
		// Find largest
		val result: Int = (Math.abs(a - b) + (a + b)) / 2;
		// Display given numbers
		println(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		println(" Largest : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: FindLargest = FindLargest();
	// Test Cases
	task.largest(23, 2);
	task.largest(-5, -3);
	task.largest(52, 234);
	task.largest(-1, -4);
}

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1

Another solution.

// C program for 
// Find largest number without using relational operator
// Set B
#include <stdio.h>

void largest(int a, int b)
{
   // Find largest
   int result =  a - ((a - b) & ((a - b) >> 31));
   // Display given numbers
   printf(" Given number a : %d  b : %d \n",a,b);
   // Display the calculated largest number
   printf(" Largest : %d\n", result);
}


int main(int argc, char const *argv[])
{
    // Test cases
    largest(23,2);
    largest(-5,-3);
    largest(52,234);
    largest(-1,-4);

    return 0;
}

input

 Given number a : 23  b : 2
 Largest : 23
 Given number a : -5  b : -3
 Largest : -3
 Given number a : 52  b : 234
 Largest : 234
 Given number a : -1  b : -4
 Largest : -1
/*
  Java Program for 
  Find largest number without using relational operator
  Set B
*/
public class FindLargest
{
	public void largest(int a, int b)
	{
		// Find largest
		int result = a - ((a - b) & ((a - b) >> 31));
		// Display given numbers
		System.out.println(" Given number a : " + a + " b : " + b );
		// Display the calculated largest number
		System.out.println(" Largest : " + result );
	}
	public static void main(String[] args)
	{
		FindLargest task = new FindLargest();
		// Test Cases
		task.largest(23, 2);
		task.largest(-5, -3);
		task.largest(52, 234);
		task.largest(-1, -4);
	}
}

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for 
  Find largest number without using relational operator
  Set B
*/
class FindLargest
{
	public: void largest(int a, int b)
	{
		// Find largest
		int result = a - ((a - b) &((a - b) >> 31));
		// Display given numbers
		cout << " Given number a : " << a << " b : " << b << endl;
		// Display the calculated largest number
		cout << " Largest : " << result << endl;
	}
};
int main()
{
	FindLargest *task = new FindLargest();
	// Test Cases
	task->largest(23, 2);
	task->largest(-5, -3);
	task->largest(52, 234);
	task->largest(-1, -4);
	return 0;
}

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
// Include namespace system
using System;
/*
  Csharp Program for 
  Find largest number without using relational operator
  Set B
*/
public class FindLargest
{
	public void largest(int a, int b)
	{
		// Find largest
		int result = a - ((a - b) & ((a - b) >> 31));
		// Display given numbers
		Console.WriteLine(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		Console.WriteLine(" Largest : " + result);
	}
	public static void Main(String[] args)
	{
		FindLargest task = new FindLargest();
		// Test Cases
		task.largest(23, 2);
		task.largest(-5, -3);
		task.largest(52, 234);
		task.largest(-1, -4);
	}
}

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
<?php
/*
  Php Program for 
  Find largest number without using relational operator
  Set B
*/
class FindLargest
{
	public	function largest($a, $b)
	{
		// Find largest
		$result = $a - (($a - $b) & (($a - $b) >> 31));
		// Display given numbers
		echo " Given number a : ".$a.
		" b : ".$b.
		"\n";
		// Display the calculated largest number
		echo " Largest : ".$result.
		"\n";
	}
}

function main()
{
	$task = new FindLargest();
	// Test Cases
	$task->largest(23, 2);
	$task->largest(-5, -3);
	$task->largest(52, 234);
	$task->largest(-1, -4);
}
main();

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
/*
  Node JS Program for 
  Find largest number without using relational operator
  Set B
*/
class FindLargest
{
	largest(a, b)
	{
		// Find largest
		var result = a - ((a - b) & ((a - b) >> 31));
		// Display given numbers
		console.log(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		console.log(" Largest : " + result);
	}
}

function main()
{
	var task = new FindLargest();
	// Test Cases
	task.largest(23, 2);
	task.largest(-5, -3);
	task.largest(52, 234);
	task.largest(-1, -4);
}
main();

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
#  Python 3 Program for 
#  Find largest number without using relational operator
#  Set B
class FindLargest :
	def largest(self, a, b) :
		result = a - ((a - b) & ((a - b) >> 31))
		#  Display given numbers
		print(" Given number a : ", a ," b : ", b)
		#  Display the calculated largest number
		print(" Largest : ", result)
	

def main() :
	task = FindLargest()
	#  Test Cases
	task.largest(23, 2)
	task.largest(-5, -3)
	task.largest(52, 234)
	task.largest(-1, -4)

if __name__ == "__main__": main()

input

 Given number a :  23  b :  2
 Largest :  23
 Given number a :  -5  b :  -3
 Largest :  -3
 Given number a :  52  b :  234
 Largest :  234
 Given number a :  -1  b :  -4
 Largest :  -1
#  Ruby Program for 
#  Find largest number without using relational operator
#  Set B
class FindLargest 
	def largest(a, b) 
		#  Find largest
		result = a - ((a - b) & ((a - b) >> 31))
		#  Display given numbers
		print(" Given number a : ", a ," b : ", b, "\n")
		#  Display the calculated largest number
		print(" Largest : ", result, "\n")
	end

end

def main() 
	task = FindLargest.new()
	#  Test Cases
	task.largest(23, 2)
	task.largest(-5, -3)
	task.largest(52, 234)
	task.largest(-1, -4)
end

main()

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
/*
  Scala Program for 
  Find largest number without using relational operator
  Set B
*/
class FindLargest()
{
	def largest(a: Int, b: Int): Unit = {
		// Find largest
		var result: Int = a - ((a - b) & ((a - b) >> 31));
		// Display given numbers
		println(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		println(" Largest : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: FindLargest = new FindLargest();
		// Test Cases
		task.largest(23, 2);
		task.largest(-5, -3);
		task.largest(52, 234);
		task.largest(-1, -4);
	}
}

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1
/*
  Swift 4 Program for 
  Find largest number without using relational operator
  Set B
*/
class FindLargest
{
	func largest(_ a: Int, _ b: Int)
	{
		// Find largest
		let result: Int = a - ((a - b) & ((a - b) >> 31));
		// Display given numbers
		print(" Given number a : ", a ," b : ", b);
		// Display the calculated largest number
		print(" Largest : ", result);
	}
}
func main()
{
	let task: FindLargest = FindLargest();
	// Test Cases
	task.largest(23, 2);
	task.largest(-5, -3);
	task.largest(52, 234);
	task.largest(-1, -4);
}
main();

input

 Given number a :  23  b :  2
 Largest :  23
 Given number a :  -5  b :  -3
 Largest :  -3
 Given number a :  52  b :  234
 Largest :  234
 Given number a :  -1  b :  -4
 Largest :  -1
/*
  Kotlin Program for 
  Find largest number without using relational operator
  Set B
*/
class FindLargest
{
	fun largest(a: Int, b: Int): Unit
	{
		// Find largest
		val result: Int = a - ((a - b) and((a - b) shr 31));
		// Display given numbers
		println(" Given number a : " + a + " b : " + b);
		// Display the calculated largest number
		println(" Largest : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: FindLargest = FindLargest();
	// Test Cases
	task.largest(23, 2);
	task.largest(-5, -3);
	task.largest(52, 234);
	task.largest(-1, -4);
}

input

 Given number a : 23 b : 2
 Largest : 23
 Given number a : -5 b : -3
 Largest : -3
 Given number a : 52 b : 234
 Largest : 234
 Given number a : -1 b : -4
 Largest : -1


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