Write a java program to find largest of three numbers using ternary operator in java. Given a three input numbers, we have to write a java code to print largest of three numbers using ternary operator.

Suppose, if we have three numbers a, b and c. If a is greater than b and c then a is the largest of three numbers. Similarly, if b is greater than a and c then b is the largest of three numbers else c is largest.

### Ternary Operator

A ternary operator evaluates a boolean expression and assign the value based on the result. Let’s take a example for better understanding.

**result = (expression) ? value if true : value if false**

int result = (3 > 2) ? 3 : 2;

In the above statement, we are checking if 3 is greater than 2. If this expression evaluates to true then 3 is assign in a result variable else 2 is assign.

## Find Largest of Three Numbers using Ternary Operator in Java

In this java code, we have used the ternary operator to first compare a with b. If this expression evaluates to true then variable a is compared with c. If it’s true then the value of a is assigned to a result variable. Similarly, In else condition we are comparing the value b like this.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Print largest of three numbers using ternary operator in java */ public class LargestOfThree { public static void main(String[] args) { //Three input numbers int a = 55; int b = 95; int c = 75; int result = ( a > b ) ? (a > c ? a : c) : ( b > c ? b : c); System.out.println(result); } } |

In this video tutorial, i have explained how to print largest of three numbers using ternary operator and if else condition.