Largest of two distinct numbers without using any conditional statements or operators - GeeksforGeeks

01 Apr, 2021
Given two positive and distinct numbers, the task is to find the greatest of two given numbers without using any conditional statements(if...) and operators(?: in C/C++/Java).
Examples:  
Input: a = 14, b = 15
Output: 15

Input: a = 1233133, b = 124
Output: 1233133
The Approach is to return the value on the basis of the below expression: 
a * (bool)(a / b) + b * (bool)(b / a)  
The expression a / b will give 1 if a > b and 0 if a < b (only after typecasting the result to bool). Hence, the answer will be of the form either a + 0 or 0 + b depending upon which one is greater. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program for above implementation#include <iostream>using namespace std; // Function to find the largest numberint largestNum(int a, int b){    return a * (bool)(a / b) + b * (bool)(b / a);} // Drivers codeint main(){    int a = 22, b = 1231;    cout << largestNum(a, b);     return 0;}
// Java program for above implementationclass GFG{     // Function to find the largest number    static int largestNum(int a, int b)    {        return a * ((a / b) > 0 ? 1 : 0) + b * ((b / a) > 0 ? 1 : 0);    }     // Drivers code    public static void main(String[] args)    {        int a = 22, b = 1231;        System.out.print(largestNum(a, b));    }} // This code is contributed by 29AjayKumar
# Function to find the largest numberdef largestNum(a, b):    return a * (bool)(a // b) + \           b * (bool)(b // a); # Driver Codea = 22;b = 1231;print(largestNum(a, b)); # This code is contributed by Rajput-Ji
     // C# program for above implementationusing System; class GFG{     // Function to find the largest number    static int largestNum(int a, int b)    {        return a * ((a / b) > 0 ? 1 : 0) + b * ((b / a) > 0 ? 1 : 0);    }     // Driver code    public static void Main(String[] args)    {        int a = 22, b = 1231;        Console.Write(largestNum(a, b));    }} // This code is contributed by Rajput-Ji
<?php// PHP program for above implementation // Function to find the largest numberfunction largestNum($a, $b){    return ($a * (boolean)floor(($a / $b))) +           ($b * (boolean)floor(($b / $a)));} // Drivers code$a = 22; $b = 1231;echo(largestNum($a, $b)); // This code is contributed// by Mukul Singh
<script> // Javascript program for above implementation // Function to find the largest numberfunction largestNum(a , b){    return a * (parseInt(a / b) > 0 ? 1 : 0) +           b * (parseInt(b / a) > 0 ? 1 : 0);} // Driver codevar a = 22, b = 1231; document.write(largestNum(a, b)); // This code is contributed by shikhasingrajput </script>
1231
 
Code_Mech
29AjayKumar
Rajput-Ji
shikhasingrajput
Mathematical
Misc
School Programming
Misc
Mathematical
Misc
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Program to convert a given number to words
Program to multiply two matrices
Modular multiplicative inverse
Program to print prime numbers from 1 to N.
vector::push_back() and vector::pop_back() in C++ STL
Top 10 algorithms in Interview Questions
Overview of Data Structures | Set 1 (Linear Data Structures)
How to write Regular Expressions?
Software Engineering | Prototyping Model