BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to find largest of three numbers using Ternary Operator

Last Updated: September 9, 2017 by Chaitanya Singh | Filed Under: Java Examples

This program finds the largest of three numbers using ternary operator. Before going through the program, lets understand what is a ternary Operator:
Ternary operator evaluates a boolean expression and assign the value based on the result.

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

If the expression results true then the first value before the colon (:) is assigned to the variable num else the second value is assigned to the num.

Example: Program to find the largest number using ternary operator

In this Program, we have used the ternary operator twice to compare the three numbers, however you can compare all three numbers in one statement using ternary operator like this:

result = num3 > (num1>num2 ? num1:num2) ? num3:((num1>num2) ? num1:num2);

However if you are finding it difficult to understand then use it like I have shown in the example below:

import java.util.Scanner;
public class JavaExample 
{
    public static void main(String[] args) 
    {
        int num1, num2, num3, result, temp;
        /* Scanner is used for getting user input. 
         * The nextInt() method of scanner reads the
         * integer entered by user.
         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter First Number:");
        num1 = scanner.nextInt();
        System.out.println("Enter Second Number:");
        num2 = scanner.nextInt();
        System.out.println("Enter Third Number:");
        num3 = scanner.nextInt();
        scanner.close();
       
        
        /* In first step we are comparing only num1 and
         * num2 and storing the largest number into the
         * temp variable and then comparing the temp and
         * num3 to get final result.
         */
        temp = num1>num2 ? num1:num2;
        result = num3>temp ? num3:temp;
        System.out.println("Largest Number is:"+result);
    }
}

Output:

Enter First Number:
89
Enter Second Number:
109
Enter Third Number:
8
Largest Number is:109

Top Related Articles:

  1. Java Program to Perform Arithmetic operation using Method Overloading
  2. Java Program to Swap two numbers using Bitwise XOR Operator
  3. Java Program to Add two Binary Numbers
  4. Java Program to Calculate average using Array
  5. Java Program to find the Smallest of three numbers using Ternary Operator

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap