Q:
Java Program to Find Smallest of Three Numbers Using Ternary Operator
belongs to collection: Java Number Programs
Java Number Programs
- How to Reverse a Number in Java
- Java Program Number to Word
- Automorphic Number Program in Java
- Peterson Number in Java
- Sunny Number in Java
- Tech Number in Java
- Fascinating Number in Java
- Keith Number in Java
- Neon Number in Java
- Spy Number in Java
- ATM program Java
- Autobiographical Number in Java
- Emirp Number in Java
- Sphenic Number in Java
- Buzz Number Java
- Duck Number Java
- Evil Number Java
- ISBN Number Java
- Krishnamurthy Number Java
- Bouncy Number in Java
- Mystery Number in Java
- Smith Number in Java
- Strontio Number in Java
- Xylem and Phloem Number in Java
- nth Prime Number Java
- Java Program to Display Alternate Prime Numbers
- Java Program to Find Square Root of a Number Without sqrt Method
- Java Program to Swap Two Numbers Using Bitwise Operator
- Java Program to Find GCD of Two Numbers
- Java Program to Find Largest of Three Numbers
- Java Program to Find Smallest of Three Numbers Using Ternary Operator
- Java Program to Check if a Number is Positive or Negative
- Java Program to Check if a Given Number is Perfect Square
- Java Program to Display Even Numbers From 1 to 100
- Java Program to Display Odd Numbers From 1 to 100
- Java Program to Find Sum of Natural Numbers
SmallestNumberExample1.java
Output:
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.
SmallestNumberExample2.java
Output:
Using if-else..if
SmallestNumberExample3.java
Output:
Using nested-if Statement
SmallestNumberExample4.java
Output:
Let's see another logic to find the smallest of three numbers.
SmallestNumberExample5.java
Output 1:
Output 2: