Java Program to Find Smallest of Three Numbers Using Ternary Operator


In this section, we will learn how to create a Java program to find the smallest of three numbers. Along with this, we will also learn how to find the smallest of three numbers in Java using the ternary operator.

Using Ternary Operator

Before moving to the program, let’s understand the ternary operator.

It is a part of the Java conditional statement. It contains three operands. It evaluates a Boolean expression and assigned a value based on the result. We can use it in place of the if-else statement. The syntax of the ternary operator is as follows:

  1. variable_name = (expression) ? value if true:value if false  

If the above expression returns true the value before the colon is assigned to the variable variable_name, else the value after the colon is assigned to the variable.

Let’s use the ternary operator in a Java program to compare three variables.

In the following program, we have used two ternary operators to compare three numbers.

SmallestNumberExample1.java

  1. import java.util.Scanner;  
  2. public class SmallestNumberExample1  
  3. {  
  4. public static void main(String[] args)   
  5. {  
  6. int a, b, c, smallest, temp;  
  7. //object of the Scanner class  
  8. Scanner sc = new Scanner(System.in);  
  9. //reading input from the user  
  10. System.out.println(“Enter the first number:”);  
  11. a = sc.nextInt();  
  12. System.out.println(“Enter the second number:”);  
  13. b = sc.nextInt();  
  14. System.out.println(“Enter the third number:”);  
  15. c = sc.nextInt();  
  16. //comparing a and b and storing the smallest number in a temp variable  
  17. temp=a<b?a:b;  
  18. //comparing the temp variable with c and storing the result in the variable names smallest  
  19. smallest=c<temp?c:temp;  
  20. //prints the smallest number  
  21. System.out.println(“The smallest number is: “+smallest);  
  22. }  
  23. }  

Output:

Enter the first number:
23
Enter the second number:
11
Enter the third number:
67
The smallest Number is: 11

We can also compare all the three numbers by using the ternary operator in a single statement. If we want to compare three numbers in a single statement, we should use the following statement:

  1. d = c > (a>b ? a:b) ? c:((a>b) ? a:b);  

In the following program, we have used a single statement to find the smallest of three numbers.

SmallestNumberExample2.java

  1. import java.util.Scanner;  
  2. public class SmallestNumberExample2  
  3. {  
  4. public static void main(String[] args)   
  5. {  
  6. int a, b, c, smallest;  
  7. //object of the Scanner class  
  8. Scanner sc = new Scanner(System.in);  
  9. //reading input from the user  
  10. System.out.println(“Enter the first number:”);  
  11. a = sc.nextInt();  
  12. System.out.println(“Enter the second number:”);  
  13. b = sc.nextInt();  
  14. System.out.println(“Enter the third number:”);  
  15. c = sc.nextInt();  
  16. smallest = c < (a < b ? a : b) ? c : ((a < b) ? a : b);  
  17. System.out.println(“The smallest number is: “+smallest);  
  18. }  
  19. }  

Output:

Enter the first number:
45
Enter the second number:
87
Enter the third number:
34
The smallest number is: 34

Using if-else..if

SmallestNumberExample3.java

  1. public class SmallestNumberExample3  
  2. {  
  3. public static void main(String[] args)   
  4. {  
  5. //initializing numbers to compare  
  6. int a=101, b=8, c=19;  
  7. //comparing numbers, a with b and a with c   
  8. //if both conditions are true, prints a  
  9. if(a<=b && a<=c)  
  10. System.out.println(a+” is the smallest number”);  
  11. //comparing b with a and b with c  
  12. //if both conditions are true, prints b  
  13. else if (b<=a && b<=c)  
  14. System.out.println(b+” is the smallest number”);  
  15. else  
  16. //prints c if the above conditions are false  
  17. System.out.println(c+” is the smallest number”);  
  18. }  
  19. }  

Output:

8 is the smallest number

Using nested-if Statement

SmallestNumberExample4.java

  1. public class SmallestNumberExample4  
  2. {  
  3. public static void main(String[] args)   
  4. {  
  5. //initializing numbers to compare  
  6. int x = 110, y = 170, z = 169;  
  7. if(x<=y)    
  8. {  
  9. if(x<=z)  
  10. //prints x, if the above two conditions are true  
  11. System.out.println(“The smallest number is: “+x);  
  12. else  
  13. //prints z, if the condition defined in outer if is true and the condition defined in inner if is false  
  14. System.out.println(“The smallest number is: “+z);  
  15. }   
  16. else   
  17. {  
  18. if(y<=z)  
  19. //prints y, if the condition defined in outer if is false and the condition defined in inner if is true  
  20. System.out.println(“The smallest number is: “+y);  
  21. else  
  22. //prints z, if the condition defined in both inner and outer loop is false  
  23. System.out.println(“The smallest number is: “+z);  
  24. }  
  25. }  
  26. }  

Output:

The smallest number is: 169

Let’s see another logic to find the smallest of three numbers.

SmallestNumberExample5.java

  1. import java.util.Scanner;  
  2. public class SmallestNumberExample5  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. int num1, num2, num3;  
  7. System.out.println(“Enter three integers: “);  
  8. Scanner in = new Scanner(System.in);  
  9. num1=in.nextInt();  
  10. num2=in.nextInt();  
  11. num3=in.nextInt();  
  12. if (num1 < num2 && num1 < num3)  
  13. System.out.println(“The smallest number is: “+num1);  
  14. else if (num2 < num1 && num2 < num3)  
  15. System.out.println(“The smallest number is: “+num2);  
  16. else if (num3 < num1 && num3 < num2)  
  17. System.out.println(“The smallest number is: “+num3);  
  18. else  
  19. System.out.println(“The numbers are same.”);  
  20. }  
  21. }  

Output 1:

Enter three integers: 
12 110 9
The smallest number is: 9

Output 2:

Enter three integers: 
12 12 12
The numbers are same.

Leave a Comment