Find Largest of Three Numbers using Ternary Operator in Java

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.

Find largest of three numbers using ternary operator

Find largest of three numbers using ternary operator

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.

Subscribe Our Tutorials

Get Latest Updates on Facebook

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.

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

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , . Bookmark the permalink.