Structure of the Problem Requirements
In this problem we will learn how to use ternary operator in Java. Before this we upload ternary operator in C++. Ternary operator is used to minimize the keystrokes and increase the readability of the code. The simple if else condition and ternary operator perform the same task . This simple program return the greater number with the use of ternary operator. Here is the source code of this problem which help you in better understanding.
Source Code
import java.util.Scanner;
public class Toperator {
public static void main(String[] args)
{
int a,b,c;
Scanner input = new Scanner (System.in);
System.out.print("\t \t \t LEP Tutorials \n \n ");
System.out.print(" Enter Your Number : ");
a = input.nextInt();
System.out.print(" Enter Your Number : ");
b = input.nextInt();
c = (a > b) ? a : b;
System.out.print(" Greater Number is : "+c);
}
}
Output of the program
![]() |
Ternary Operator in Java |