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++

`// C++ program for above implementation ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to find the largest number ` `int` `largestNum(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `a * (` `bool` `)(a / b) + b * (` `bool` `)(b / a); ` `} ` ` ` `// Drivers code ` `int` `main() ` `{ ` ` ` `int` `a = 22, b = 1231; ` ` ` `cout << largestNum(a, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for above implementation ` `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` `); ` ` ` `} ` ` ` ` ` `// 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 ` |

*chevron_right*

*filter_none*

## Python3

`# Function to find the largest number ` `def` `largestNum(a, b): ` ` ` `return` `a ` `*` `(` `bool` `)(a ` `/` `/` `b) ` `+` `\ ` ` ` `b ` `*` `(` `bool` `)(b ` `/` `/` `a); ` ` ` `# Driver Code ` `a ` `=` `22` `; ` `b ` `=` `1231` `; ` `print` `(largestNum(a, b)); ` ` ` `# This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## C#

` ` `// C# program for above implementation ` `using` `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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for above implementation ` ` ` `// Function to find the largest number ` `function` `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 ` |

*chevron_right*

*filter_none*

**Output:**

1231

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- C program to check if a given year is leap year using Conditional operator
- Print a pattern without using any loop
- Loops and Control Statements (continue, break and pass) in Python
- Difference between continue and break statements in C++
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Strings formed from given characters without any consecutive repeating characters
- C program to Compare Two Strings without using strcmp() function
- Program to find LCM of 2 numbers without using GCD
- Find the largest and second largest value in a Linked List
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- Check if a Number is Odd or Even using Bitwise Operators
- Generate first K multiples of N using Bitwise operators
- Java Program to Multiply two Matrices of any size
- Swap the elements between any two given quadrants of a Matrix
- C program to Find the Largest Number Among Three Numbers
- Largest Even and Odd N-digit numbers in Hexadecimal Number System
- Largest and Smallest N-digit Hexadecimal Numbers
- What are the operators that can be and cannot be overloaded in C++?
- Increment and Decrement Operators in Python
- Division Operators in Python

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.