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

Java program to find largest in three numbers using ternary operator

Find largest number using conditional operator in java is one of the common interview question.

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.

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

Here’s the syntax of ternary operator in java,

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

If expression is true then ternary operator returns first value else second value is returned. Now let’s see java program,

import java.util.Scanner;

public class LargestInThree 
{
   public static void main(String[] args) 
   {
      int number1, number2, number3, output;
      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();
      output = number3 > (number1 > number2 ? number1 : number2) ? number3 : ((number1 > number2) ? number1 : number2);
      System.out.println("Find largest in three numbers using ternary operator: " + output);
      sc.close();
   }
}


Output:

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


Biggest of 2 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.

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.

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

Let’s learn java program to find largest of two numbers using ternary operator,

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.print("Please enter first number: ");
      num1 = sc.nextInt();
      System.out.print("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


Write a java program which should display maximum and minimum number of given 3 numbers

Now let’s learn java program to find the largest and smallest of three integers,

import java.util.Scanner;

public class MaxAndMinNumbers 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number: ");
      int number1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      int number2 = sc.nextInt();
      System.out.println("Please enter third number: ");
      int number3 = sc.nextInt();
      int maximum = displayMaximum(number1, number2, number3);
      int minimum = displayMinimum(number1, number2, number3);
      System.out.println("Largest of three numbers is: " + maximum);
      System.out.println("Smallest of three numbers is: " + minimum);
      sc.close();
   }

   public static int displayMaximum(int num1, int num2, int num3) 
   {
      int max = num1;
      if(num2 > max) 
      {
         max = num2;
      }
      if(num3 > max) 
      {
         max = num3;
      }
      return max;
   }

   public static int displayMinimum(int num1, int num2, int num3) 
   {
      int min = num1;
      if(num2 < min) 
      {
         min = num2;
      }
      if(num3 < min) 
      {
         min = num3;
      }
      return min;
   }
}


Output:

Please enter first number: 98
Please enter second number: 27
Please enter third number: 18
Largest of three numbers is: 98
Smallest of three numbers is: 18


Java program to find the 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, output, 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;
      output = number3 < temp ? number3 : temp;
      System.out.println("Smallest of three numbers using ternary operator: " + output);
   }
}


Output:

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