 # Java Program to find smallest of three numbers using ternary operator

Here we are explaining how to write a java program to find out the smallest of three numbers using the ternary operator. We have to accept three numbers from the user into the variable a,b,c. Then by using the ternary operator we will find out the smallest of a,b,c.

## What is the conditional operator or ternary operator?

The conditional operator is also known as the ternary operator. The ternary operator has mainly three operands. The ternary operator can be written as,

Variable v=(Condition)?if the condition is true v have this value: if the condition is false v have this value;

Example:  `variable t = a < b ? a:b;`

Here if a < b then t becomes a.Else t becomes b.

## How to implement the java program to find out the smallest of three numbers using ternary operators?

First we have to declare the class Smallest .Declare the integer variables a,b,c,s,t.Read three numbers from the user into the variable a,b,c.Then by using the ternary operator check a < b,if true then t =a,otherwise t=b.Then using another ternary operator check c < t ,if true  then s = c  otherwise s = t.Then display s.

## ALGORITHM

STEP 1: Declare the class Smallest with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the` `main()

STEP 3: Declare the integer variables a,b,c,s,t as integer.

STEP 4: Read the first number into the variable a.

STEP 5: Read the second number into the variable b.

STEP 6: Read the third number into the variable c.

STEP 7: By using  ternary operator check a < b,if true then t = a,else t=b.

STEP 8: By using another ternary operator check c < t,if true then set s=c,else s= t.

STEP 9: Display the smallest number as s.

## Java Source Code

```                                          ```import java.util.Scanner;
public class Smallest
{
public static void main(String[] args)
{
int a,b,c,s,t;
Scanner sc = new Scanner(System.in);
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();
sc.close();
t = a < b ? a:b;
s = c < t ? c:t;
System.out.println("The Smallest Number is:"+s);
}
}```
```

## OUTPUT

```Enter The First Number:10
Enter The Second Number:2
Enter The Third Number:99
The Smallest Number is:2 ```
VIEW ALL
VIEW ALL
##### OtherTutorials
VIEW ALL 