Java program to find largest in three numbers using ternary operator

Let’s learn java program to find largest in three numbers using ternary operator.

Java program to find largest in three numbers using ternary operator

Write a program to find largest of three numbers by using ternary operators is one of the common interview question.

java program to find largest in three numbers using ternary operator

Java ternary operator is an alternative for if-else statement.

In the below java program user enters three numbers.

Then ternary operator is used to find largest number.

Here’s the syntax of ternary operator in java,

output = (expression) ? value1 (if true) : value2 (if false)


If expression is true then ternary operator returns first value else second value is returned.

Also read – java program to find largest of three numbers using nested if

Now let’s see java program to find largest in three numbers using ternary operator.

import java.util.Scanner;
public class LargestInThree 
{
   public static void main(String[] args) 
   {
      int number1, number2, number3, largest;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number: ");
      number1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      number2 = sc.nextInt();
      System.out.println("Please enter third number: ");
      number3 = sc.nextInt();
      largest = number3 > (number1 > number2 ? number1 : number2) ? number3 : ((number1 > number2) ? number1 : number2);
      System.out.println("Largest in three numbers using ternary operator: " + largest);
      sc.close();
   }
}


Output:

Please enter first number: 665
Please enter second number: 268
Please enter third number: 263
Largest in three numbers using ternary operator: 665


Largest among two numbers using ternary operator in java

In the below java program we take input from user using scanner class. First user enters two numbers.

These two numbers are stored in two integer variables num1 and num2.

In the next step if condition checks whether user entered numbers are equal or not.

Also read – continue statement in java

If two numbers are equal then print two numbers are equal.

Else using conditional operator or ternary operator find biggest of 2 numbers.

Now using ternary operator or conditional operator we check if first number is greater than second number.

Now in ternary operator if condition is true ternary operator returns first value after “?” symbol, that is, num1.

Also read – how to create directory in java

Now if condition is false ternary operator returns value after “:” symbol, that is, num2.

Let’s learn java program to find largest among two numbers using ternary operator in java.

import java.util.Scanner;
public class LargestOfTwoNumbers 
{
   public static void main(String[] args) 
   {
      int num1, num2, largest;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number: ");
      num1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      num2 = sc.nextInt();
      if(num1 == num2)
      {
         System.out.println("two numbers are equal");     
      }
      else
      {
         largest = (num1 > num2) ? num1 : num2;
         System.out.println("Biggest of 2 numbers using ternary operator: " + largest);   
      }
      sc.close();
   }
}


Output:

Please enter first number: 29
Please enter second number: 18
Biggest of 2 numbers using ternary operator: 29

Please enter first number: 24
Please enter second number: 24
two numbers are equal


smallest of three numbers using ternary operator

Here’s the program to find the smallest of three numbers using ternary operator.

import java.util.Scanner;
public class SmallestOfThreeNumbers 
{
   public static void main(String[] args) 
   {
      int number1, number2, number3, smallest, temp;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number: ");
      number1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      number2 = sc.nextInt();
      System.out.println("Please enter third number: ");
      number3 = sc.nextInt();
      sc.close();
      temp = number1 < number2 ? number1 : number2;
      smallest = number3 < temp ? number3 : temp;
      System.out.println("Smallest of three numbers using ternary operator: " + smallest);
   }
}


Output:

Please enter first number: 18
Please enter second number: 73
Please enter third number: 89
Smallest of three numbers using ternary operator: 18