In this section, we will explore the process of developing a Java program to determine the smallest of three numbers. Additionally, we will also examine how to find the smallest of three numbers in Java by utilizing the ternary operator.
Using Ternary Operator
Before we proceed with the program, let’s take a moment to understand the ternary operator in Java.
The ternary operator is an integral part of Java’s conditional statement. It consists of three operands and is responsible for evaluating a Boolean expression. Based on the result, it assigns a value accordingly. The ternary operator can be used as a concise alternative to an if-else statement. Its syntax is as follows:
variable_name = (expression) ? value if true:value if false
If the condition specified in the ternary expression is true, the value preceding the colon is assigned to the variable. Otherwise, the value following the colon is assigned to the variable.
Now, let’s apply the ternary operator in a Java program to compare three variables.
In the provided program, we have utilized two ternary operators to compare three numbers.
SmallestNumberExample.java
import java.util.Scanner; public class SmallestNumberExample { public static void main(String[] args) { int a, b, c, smallest, temp; //object of the Scanner class Scanner sc = new Scanner(System.in); //reading input from the user System.out.println("Enter the first number:"); a = sc.nextInt(); System.out.println("Enter the second number:"); b = sc.nextInt(); System.out.println("Enter the third number:"); c = sc.nextInt(); //comparing a and b and storing the smallest number in a temp variable temp=a<b?a:b; //comparing the temp variable with c and storing the result in the variable names smallest smallest=c<temp?c:temp; //prints the smallest number System.out.println("The smallest number is: "+smallest); } }
Output:
Enter the first number: 23 Enter the second number: 11 Enter the third number: 67 The smallest Number is: 11
To compare all three numbers in a single statement using the ternary operator, we can employ the following statement.
d = c > (a>b ? a:b) ? c:((a>b) ? a:b);
SmallestNumberExample1.java
import java.util.Scanner; public class SmallestNumberExample1 { public static void main(String[] args) { int a, b, c, smallest; //object of the Scanner class Scanner sc = new Scanner(System.in); //reading input from the user System.out.println("Enter the first number:"); a = sc.nextInt(); System.out.println("Enter the second number:"); b = sc.nextInt(); System.out.println("Enter the third number:"); c = sc.nextInt(); smallest = c < (a < b ? a : b) ? c : ((a < b) ? a : b); System.out.println("The smallest number is: "+smallest); } }
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
SmallestNumberExample2.java
public class SmallestNumberExample2 { public static void main(String[] args) { //initializing numbers to compare int a=101, b=8, c=19; //comparing numbers, a with b and a with c //if both conditions are true, prints a if(a<=b && a<=c) System.out.println(a+" is the smallest number"); //comparing b with a and b with c //if both conditions are true, prints b else if (b<=a && b<=c) System.out.println(b+" is the smallest number"); else //prints c if the above conditions are false System.out.println(c+" is the smallest number"); } }
Output:
8 is the smallest number
Using nested-if Statement
SmallestNumberExample3.java
public class SmallestNumberExample3 { public static void main(String[] args) { //initializing numbers to compare int x = 110, y = 170, z = 169; if(x<=y) { if(x<=z) //prints x, if the above two conditions are true System.out.println("The smallest number is: "+x); else //prints z, if the condition defined in outer if is true and the condition defined in inner if is false System.out.println("The smallest number is: "+z); } else { if(y<=z) //prints y, if the condition defined in outer if is false and the condition defined in inner if is true System.out.println("The smallest number is: "+y); else //prints z, if the condition defined in both inner and outer loop is false System.out.println("The smallest number is: "+z); } } }
Output:
The smallest number is: 169
SmallestNumberExample4.java
import java.util.Scanner; public class SmallestNumberExample4 { public static void main(String args[]) { int num1, num2, num3; System.out.println("Enter three integers: "); Scanner in = new Scanner(System.in); num1=in.nextInt(); num2=in.nextInt(); num3=in.nextInt(); if (num1 < num2 && num1 < num3) System.out.println("The smallest number is: "+num1); else if (num2 < num1 && num2 < num3) System.out.println("The smallest number is: "+num2); else if (num3 < num1 && num3 < num2) System.out.println("The smallest number is: "+num3); else System.out.println("The numbers are same."); } }
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.
For further information on ‘Find Smallest of Three Numbers Using Ternary Operator in Java’, you can visit tutorials.freshersnow.com.