Posted on by Kalkicode
Code Mathematics

# Find largest number without using relational operator

The approach to finding the largest number among two numbers without using relational operators uses bit manipulation to determine the larger number. Here's how it works:

1. We take two input integers, `a` and `b`.

2. We subtract `b` from `a` to get the difference between the two numbers. If `a` is greater than `b`, then the difference will be positive. Otherwise, it will be negative.

3. We extract the sign bit of the difference using bit manipulation. Specifically, we right shift the difference by 31 bits (assuming a 32-bit integer) and perform a bitwise AND with 1 to get the sign bit. The sign bit will be 0 if the difference is non-negative, and 1 if the difference is negative.

4. We use the sign bit to determine which number is larger. If the sign bit is 0, then `a` is larger than `b`, so we simply output `a`. Otherwise, `b` is larger than `a`, so we subtract the difference from `a` to get the larger number.

5. We output the larger number.

Note that this approach relies on the fact that the sign bit of a negative number in two's complement representation is 1, and the sign bit of a non-negative number is 0. It also assumes that the difference between the two numbers can be represented in a 32-bit integer without overflow.

## Program Solution

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

## Comment

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

Edu Seekho     58 Day ago
``Nice explanation on <a href="https://eduseekho.com/c-program-to-find-largest-of-three-numbers/">C Program To Find Largest of Three Numbers</a>! If you're interested, I've also covered this topic on my blog. Check it out for more insights!``
Categories
Relative Post