Subject - Computer
Language - Java
Topic - Ternary Operator
-------------------------------------
Write a program to find the lowest in three numbers using ternary operator.
Answers
The given problem is solved using language - Java.
import java.util.Scanner;
public class Lowest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a,b,c,min;
System.out.print("Enter first number: ");
a=sc.nextInt();
System.out.print("Enter second number: ");
b=sc.nextInt();
System.out.print("Enter third number: ");
c=sc.nextInt();
sc.close();
min=a < b ? a < c ? a : c : b < c ? b : c;
System.out.println("Minimum: "+min);
}
}
At first, we will solve this using simple if-else statements. The code goes here:
if(a < b) { #block 1
if(a < c)
min=a;
else
min=c;
}
else{ #block 2
if(b < c)
min=b;
else
min=c;
}
Using ternary operator, we can check the same condition.
> min = (a < b) ? #block 1 : #block 2
Now, let us complete the block 1.
if(a < c)
min=a;
else
min=c;
---- OR ----
> min = a < c : a : c;
Similarly, for block 2, we get:
> min = b < c : b : c;
Combining both (1) and (2), we get:
> min=a < b ? a < c ? a : c : b < c ? b : c;
↑ block 1 ↑ block 2
In this way, the problem is solved.
See attachment for output.
Answer:
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:
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
import java.util.Scanner;
public class SmallestNumberExample1
{
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
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:
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.