Java program to find largest of the three numbers using ternary operators

Java Programming Java8Object Oriented Programming

The conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as −

variable x = (expression) ? value if true : value if false

Example

Live Demo

public class LargestOf3Nums_TernaryOperator {
   public static void main(String args[]) {
      int a, b, c, temp, result;
      a = 10;
      b = 20;
      c = 30;

      temp = a < b ? a:b;
      result = c < temp ? c:temp;
      System.out.println("Largest number is ::"+result);
   }
}

Output

Largest number is ::30




raja
Published on 26-Apr-2018 07:59:00
Advertisements