Smallest of three integers without comparison operators

Here given code implementation process.

// C program for
// Smallest of three integers without comparison operators
#include <stdio.h>

// Return a minimum value of given two numbers
int minValue(int a, int b)
{
  return b + (((a - b) >> (sizeof(int) *7)) & (a - b));
}
// Handles the request of finding minimum of given three numbers
void smallest(int a, int b, int c)
{
  // Display given value
  printf(" A : %d\n", a);
  printf(" B : %d\n", b);
  printf(" C : %d\n", c);
  int result = minValue(a, minValue(b, c));
  // Display calculated minimum
  printf(" Minimum is : %d\n\n", result);
}
int main()
{
  // Test cases
  smallest(3, -2, -6);
  smallest(5, 9, 7);
  smallest(-2, -2, -1);
  smallest(12, 3, 4);
  return 0;
}

Output

 A : 3
 B : -2
 C : -6
 Minimum is : -6

 A : 5
 B : 9
 C : 7
 Minimum is : 5

 A : -2
 B : -2
 C : -1
 Minimum is : -2

 A : 12
 B : 3
 C : 4
 Minimum is : 3
/*
  Java Program for
  Smallest of three integers without comparison operators
*/
class Comparison
{
  // Return a minimum value of given two numbers
  public int minValue(int a, int b)
  {
    return b + (((a - b) >> (Integer.SIZE-1)) & (a - b));
  }
  // Handles the request of finding minimum of given three numbers
  public void smallest(int a, int b, int c)
  {
    // Display given value
    System.out.print(" A : " + a + "\n");
    System.out.print(" B : " + b + "\n");
    System.out.print(" C : " + c + "\n");
    int result = minValue(a, minValue(b, c));
    // Display calculated minimum
    System.out.print(" Minimum is : " + result + "\n\n");
  }
  public static void main(String[] args)
  {
    Comparison task = new Comparison();
    // Test cases
    task.smallest(3, -2, -6);
    task.smallest(5, 9, 7);
    task.smallest(-2, -2, -1);
    task.smallest(12, 3, 4);
  }
}

Output

 A : 3
 B : -2
 C : -6
 Minimum is : -6

 A : 5
 B : 9
 C : 7
 Minimum is : 5

 A : -2
 B : -2
 C : -1
 Minimum is : -2

 A : 12
 B : 3
 C : 4
 Minimum is : 3
// Include header file
#include <iostream>

#include<limits.h>

using namespace std;
/*
  C++ Program for
  Smallest of three integers without comparison operators
*/
class Comparison
{
  public:
    // Return a minimum value of given two numbers
    int minValue(int a, int b)
    {
      return b + (((a - b) >> (sizeof(int) *7)) &(a - b));
    }
  // Handles the request of finding minimum of given three numbers
  void smallest(int a, int b, int c)
  {
    // Display given value
    cout << " A : " << a << "\n";
    cout << " B : " << b << "\n";
    cout << " C : " << c << "\n";
    int result = this->minValue(a, this->minValue(b, c));
    // Display calculated minimum
    cout << " Minimum is : " << result << "\n\n";
  }
};
int main()
{
  Comparison task = Comparison();
  // Test cases
  task.smallest(3, -2, -6);
  task.smallest(5, 9, 7);
  task.smallest(-2, -2, -1);
  task.smallest(12, 3, 4);
  return 0;
}

Output

 A : 3
 B : -2
 C : -6
 Minimum is : -6

 A : 5
 B : 9
 C : 7
 Minimum is : 5

 A : -2
 B : -2
 C : -1
 Minimum is : -2

 A : 12
 B : 3
 C : 4
 Minimum is : 3
// Include namespace system
using System;
/*
  C# Program for
  Smallest of three integers without comparison operators
*/
public class Comparison
{
  // Return a minimum value of given two numbers
  public int minValue(int a, int b)
  {
    return b + (((a - b) >> (sizeof(int) * 7)) & (a - b));
  }
  // Handles the request of finding minimum of given three numbers
  public void smallest(int a, int b, int c)
  {
    // Display given value
    Console.Write(" A : " + a + "\n");
    Console.Write(" B : " + b + "\n");
    Console.Write(" C : " + c + "\n");
    int result = minValue(a, minValue(b, c));
    // Display calculated minimum
    Console.Write(" Minimum is : " + result + "\n\n");
  }
  public static void Main(String[] args)
  {
    Comparison task = new Comparison();
    // Test cases
    task.smallest(3, -2, -6);
    task.smallest(5, 9, 7);
    task.smallest(-2, -2, -1);
    task.smallest(12, 3, 4);
  }
}

Output

 A : 3
 B : -2
 C : -6
 Minimum is : -6

 A : 5
 B : 9
 C : 7
 Minimum is : 5

 A : -2
 B : -2
 C : -1
 Minimum is : -2

 A : 12
 B : 3
 C : 4
 Minimum is : 3
<?php
/*
  Php Program for
  Smallest of three integers without comparison operators
*/
class Comparison
{
  // Return a minimum value of given two numbers
  public  function minValue($a, $b)
  {
    return $b + ((($a - $b) >> (8 * (PHP_INT_SIZE - 1))) & ($a - $b));
  }
  // Handles the request of finding minimum of given three numbers
  public  function smallest($a, $b, $c)
  {
    // Display given value
    echo " A : ". $a ."\n";
    echo " B : ". $b ."\n";
    echo " C : ". $c ."\n";
    $result = $this->minValue($a, $this->minValue($b, $c));
    // Display calculated minimum
    echo " Minimum is : ". $result ."\n\n";
  }
}

function main()
{
  $task = new Comparison();
  // Test cases
  $task->smallest(3, -2, -6);
  $task->smallest(5, 9, 7);
  $task->smallest(-2, -2, -1);
  $task->smallest(12, 3, 4);
}
main();

Output

 A : 3
 B : -2
 C : -6
 Minimum is : -6

 A : 5
 B : 9
 C : 7
 Minimum is : 5

 A : -2
 B : -2
 C : -1
 Minimum is : -2

 A : 12
 B : 3
 C : 4
 Minimum is : 3
/*
  Node Js Program for
  Smallest of three integers without comparison operators
*/
class Comparison
{
  // Return a minimum value of given two numbers
  minValue(a, b)
  {
    return b + (((a - b) >> (31)) & (a - b));
  }
  // Handles the request of finding minimum of given three numbers
  smallest(a, b, c)
  {
    // Display given value
    process.stdout.write(" A : " + a + "\n");
    process.stdout.write(" B : " + b + "\n");
    process.stdout.write(" C : " + c + "\n");
    var result = this.minValue(a, this.minValue(b, c));
    // Display calculated minimum
    process.stdout.write(" Minimum is : " + result + "\n\n");
  }
}

function main()
{
  var task = new Comparison();
  // Test cases
  task.smallest(3, -2, -6);
  task.smallest(5, 9, 7);
  task.smallest(-2, -2, -1);
  task.smallest(12, 3, 4);
}
main();

Output

 A : 3
 B : -2
 C : -6
 Minimum is : -6

 A : 5
 B : 9
 C : 7
 Minimum is : 5

 A : -2
 B : -2
 C : -1
 Minimum is : -2

 A : 12
 B : 3
 C : 4
 Minimum is : 3
import sys
#   Python 3 Program for
#   Smallest of three integers without comparison operators

class Comparison :
  #  Return a minimum value of given two numbers
  def minValue(self, a, b) :
    return b + (((a - b) >> (sys.getsizeof(int()) - 1)) & (a - b))
  
  #  Handles the request of finding minimum of given three numbers
  def smallest(self, a, b, c) :
    #  Display given value
    print(" A : ", a )
    print(" B : ", b )
    print(" C : ", c )
    result = self.minValue(a, self.minValue(b, c))
    #  Display calculated minimum
    print(" Minimum is : ", result ,"\n")
  

def main() :
  task = Comparison()
  #  Test cases
  task.smallest(3, -2, -6)
  task.smallest(5, 9, 7)
  task.smallest(-2, -2, -1)
  task.smallest(12, 3, 4)

if __name__ == "__main__": main()

Output

 A :  3
 B :  -2
 C :  -6
 Minimum is :  -6

 A :  5
 B :  9
 C :  7
 Minimum is :  5

 A :  -2
 B :  -2
 C :  -1
 Minimum is :  -2

 A :  12
 B :  3
 C :  4
 Minimum is :  3
#   Ruby Program for
#   Smallest of three integers without comparison operators

class Comparison 
  #  Return a minimum value of given two numbers
  def minValue(a, b) 
    return b + (((a - b) >> ( (1.size) * 8 - 1)) & (a - b))
  end

  #  Handles the request of finding minimum of given three numbers
  def smallest(a, b, c) 
    #  Display given value
    print(" A : ", a ,"\n")
    print(" B : ", b ,"\n")
    print(" C : ", c ,"\n")
    result = self.minValue(a, self.minValue(b, c))
    #  Display calculated minimum
    print(" Minimum is : ", result ,"\n\n")
  end

end

def main() 
  task = Comparison.new()
  #  Test cases
  task.smallest(3, -2, -6)
  task.smallest(5, 9, 7)
  task.smallest(-2, -2, -1)
  task.smallest(12, 3, 4)
end

main()

Output

 A : 3
 B : -2
 C : -6
 Minimum is : -6

 A : 5
 B : 9
 C : 7
 Minimum is : 5

 A : -2
 B : -2
 C : -1
 Minimum is : -2

 A : 12
 B : 3
 C : 4
 Minimum is : 3

/*
  Scala Program for
  Smallest of three integers without comparison operators
*/
class Comparison
{
  // Return a minimum value of given two numbers
  def minValue(a: Int, b: Int): Int = {
    return b + (((a - b) >> (Integer.SIZE - 1)) & (a - b));
  }
  // Handles the request of finding minimum of given three numbers
  def smallest(a: Int, b: Int, c: Int): Unit = {
    // Display given value
    print(" A : " + a + "\n");
    print(" B : " + b + "\n");
    print(" C : " + c + "\n");
    var result: Int = this.minValue(a, this.minValue(b, c));
    // Display calculated minimum
    print(" Minimum is : " + result + "\n\n");
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var task: Comparison = new Comparison();
    // Test cases
    task.smallest(3, -2, -6);
    task.smallest(5, 9, 7);
    task.smallest(-2, -2, -1);
    task.smallest(12, 3, 4);
  }
}

Output

 A : 3
 B : -2
 C : -6
 Minimum is : -6

 A : 5
 B : 9
 C : 7
 Minimum is : 5

 A : -2
 B : -2
 C : -1
 Minimum is : -2

 A : 12
 B : 3
 C : 4
 Minimum is : 3
/*
  Swift 4 Program for
  Smallest of three integers without comparison operators
*/
class Comparison
{
  // Return a minimum value of given two numbers
  func minValue(_ a: Int, _ b: Int)->Int
  {
    return b + (((a - b) >> (4 * MemoryLayout.size(ofValue:1) - 1)) & (a - b));
  }
  // Handles the request of finding minimum of given three numbers
  func smallest(_ a: Int, _ b: Int, _ c: Int)
  {
    // Display given value
    print(" A : ", a );
    print(" B : ", b );
    print(" C : ", c );
    let result: Int = self.minValue(a, self.minValue(b, c));
    // Display calculated minimum
    print(" Minimum is : ", result ,"\n");
  }
}
func main()
{
  let task: Comparison = Comparison();
  // Test cases
  task.smallest(3, -2, -6);
  task.smallest(5, 9, 7);
  task.smallest(-2, -2, -1);
  task.smallest(12, 3, 4);
}
main();

Output

 A :  3
 B :  -2
 C :  -6
 Minimum is :  -6

 A :  5
 B :  9
 C :  7
 Minimum is :  5

 A :  -2
 B :  -2
 C :  -1
 Minimum is :  -2

 A :  12
 B :  3
 C :  4
 Minimum is :  3
/*
  Kotlin Program for
  Smallest of three integers without comparison operators
*/
class Comparison
{
  // Return a minimum value of given two numbers
  fun minValue(a: Int, b: Int): Int
  {
    return b + (((a - b) shr (Integer.SIZE - 1)) and (a - b));
  }
  // Handles the request of finding minimum of given three numbers
  fun smallest(a: Int, b: Int, c: Int): Unit
  {
    // Display given value
    print(" A : " + a + "\n");
    print(" B : " + b + "\n");
    print(" C : " + c + "\n");
    var result: Int = this.minValue(a, this.minValue(b, c));
    // Display calculated minimum
    print(" Minimum is : " + result + "\n\n");
  }
}
fun main(args: Array < String > ): Unit
{
  var task: Comparison = Comparison();
  // Test cases
  task.smallest(3, -2, -6);
  task.smallest(5, 9, 7);
  task.smallest(-2, -2, -1);
  task.smallest(12, 3, 4);
}

Output

 A : 3
 B : -2
 C : -6
 Minimum is : -6

 A : 5
 B : 9
 C : 7
 Minimum is : 5

 A : -2
 B : -2
 C : -1
 Minimum is : -2

 A : 12
 B : 3
 C : 4
 Minimum is : 3
package main
import "fmt"
import "unsafe"
/*
  Go Program for
  Smallest of three integers without comparison operators
*/
type Comparison struct {}
func getComparison() * Comparison {
  var me *Comparison = &Comparison {}
  return me
}
// Return a minimum value of given two numbers
func(this Comparison) minValue(a, b int) int {
  return b + (((a - b) >> (unsafe.Sizeof(1) - 1)) & (a - b))
}
// Handles the request of finding minimum of given three numbers
func(this Comparison) smallest(a, b, c int) {
  // Display given value
  fmt.Print(" A : ", a, "\n")
  fmt.Print(" B : ", b, "\n")
  fmt.Print(" C : ", c, "\n")
  var result int = this.minValue(a, this.minValue(b, c))
  // Display calculated minimum
  fmt.Println(" Minimum is : ", result)
}
func main() {
  var task * Comparison = getComparison()
  // Test cases
  task.smallest(3, -2, -6)
  task.smallest(5, 9, 7)
  task.smallest(-2, -2, -1)
  task.smallest(12, 3, 4)
}

Output

 A : 3
 B : -2
 C : -6
 Minimum is : -6
 A : 5
 B : 9
 C : 7
 Minimum is : 5
 A : -2
 B : -2
 C : -1
 Minimum is : -2
 A : 12
 B : 3
 C : 4
 Minimum is : 3


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