Java Program to Find Largest of Three Numbers using Ternary Operator

This is a Java Program to Find Largest Between Three Numbers Using Ternary Operator.

Enter any three integer numbers as an input from which you want to find the largest integer. After that we use ternary operator(?, >, : ) to find largest number as the output.

Here is the source code of the Java Program to Find Largest Between Three Numbers Using Ternary Operator. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Largest_Ternary 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int a, b, c, d;
  7.         Scanner s = new Scanner(;
  8.         System.out.println("Enter all three numbers:");
  9.         a = s.nextInt();
  10.         b = s.nextInt();
  11.         c = s.nextInt();
  12.         d = c > (a > b ? a : b) ? c : ((a > b) ? a : b);
  13.         System.out.println("Largest Number:"+d);
  14.     }
  15. }


$ javac
$ java Largest_Ternary
Enter all three numbers:
Largest Number:7

Sanfoundry Global Education & Learning Series – 1000 Java Programs.


Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.