Java Online Compiler
Example: Greatest of Two Numbers using Ternary Operator in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Greatest of Two Numbers using Ternary Operator import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Declare two integer variables int num1 = 10; int num2 = 20; // Step 2: Use the ternary operator to find the greatest number int greatest = (num1 > num2) ? num1 : num2; // Step 3: Print the result System.out.println("The greatest number is: " + greatest); } }
Output
Clear
ADVERTISEMENTS