Java Online Compiler

Java Ternary Operators

Ternary Operator:

Ternary operator is used to perform any operation on three operands
Syntax :
Expression1 ? Expression2 : Expression3;
Here
Expression1 is a condition
Expression2 is a operation
Expression3 is a operation

In the above syntax
if condition is true ,The statements of Expression2 is execute.
if condition is false ,The statements of Expression3 is execute
Max Number Program Using Ternary Operator
Ternary Operator

import java.util.*;                               
class Main
{
 public static void main(String args[])
 {
  int x,y;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter x ,y  values");
  x=s.nextInt();
  y=s.nextInt();
  
 String  z=x>y? "x Is Max":"y Is Max";
  System.out.println(z);
 }
}
ODD or Even Number Program Using Ternary Operator

cimport java.util.*;                               
class Main
{
 public static void main(String args[])
 {
  int x;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter x values");
  x=s.nextInt();
 
 
 String  z=(x%2==0)? "Even":"Odd";
  System.out.println(z);
 }
}

Maxnumber Amoung the Three number using ternary operators
Conditional

import java.util.*;                               
class Main
{
 public static void main(String args[])
 {
  int x,y,z,p;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter x ,y ,z values");
  x=s.nextInt();
  y=s.nextInt();
  z=s.nextInt();
  p=(x>y) ?(x>z ?x : z):(y>z ? y : z);	
   System.out.println(p);
    
 }
}