Java program to find smallest of three numbers using ternary operator

Let’s learn java program to find smallest of three numbers using ternary operator.

Java program to find smallest of three numbers using ternary operator

In the below program we are using two ternary operators to find the smallest of three numbers. Ternary operator in java comprises of a condition which evaluates to either true or false and this boolean value is assigned to variable name. Here’s syntax,

variableName = (condition/expression) ? value(true) : value(false)

In the above syntax condition returns true if the value before colon(:) is assigned to variableName else value after colon(:) is assigned to variableName.

Ternary operator in java is a single line replacement for if else statement. Here’s an example 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


Now let’s see another program where we are going find smallest of three numbers in a single statement.

import java.util.Scanner;
public class SmallestOfThreeNumbersExample
{
   public static void main(String[] args)
   {
      int number1, number2, number3, smallest;
      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();
      smallest = number3 < (number1 < number2 ? number1 : number2) ? number3 : ((number1 < number2) ? number1 : number2);  
      System.out.println("Smallest of three numbers is: " + smallest);
      sc.close();
   }
}


Output:

Please enter first number: 23
Please enter second number: 72
Please enter third number: 93
Smallest of three numbers is: 23


Similarly let’s find smallest of three numbers using if-else…if

public class SmallestOfThreeNumbersExample
{
   public static void main(String[] args)
   {
      int number1 = 52, number2 = 86, number3 = 63;   
      // here if both conditions are true, print number1  
      if(number1 <= number2 && number1 <= number3)
         System.out.println(number1 + " is the smallest number.");  
      // compare number2 with number1 and number2 with number3
      // if both conditions are true, print number2
      else if(number2 <= number1 && number2 <= number3)
         System.out.println(number2 + " is the smallest number.");  
      else
         // print number3 if the above conditions are false  
         System.out.println(number3 + " is the smallest number.");
   }
}


Output:

52 is the smallest number.


Now let’s find smallest of three numbers using nested if statement.

import java.util.Scanner;
public class SmallestOfThreeNumbersExample
{
   public static void main(String[] args)
   {
      int number1, number2, number3;
      System.out.println("Please enter three numbers to find smallest number: ");  
      Scanner sc = new Scanner(System.in);  
      number1 = sc.nextInt();  
      number2 = sc.nextInt();  
      number3 = sc.nextInt();  
      if(number1 < number2 && number1 < number3)  
         System.out.println("Smallest number is: " + number1);  
      else if(number2 < number1 && number2 < number3)  
         System.out.println("Smallest number is: " + number2);  
      else if(number3 < number1 && number3 < number2)  
         System.out.println("Smallest number is: " + number3);  
      else  
         System.out.println("All the numbers are same.");
      sc.close();
   }
}


Output:

Please enter three numbers to find smallest number:
83
68
43
Smallest number is: 43

Please enter three numbers to find smallest number:
39
39
39
All the numbers are same.


Also read – preface to java virtual machine and architecture