In this tutorial, we will write a program in java to find minimum of three numbers using conditional operator. Before that, you may go through the following topics in java.
Smallest of three numbers using ternary operator in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num1, num2, num3, result; System.out.print("Enter the first Number:"); num1 = scanner.nextInt(); System.out.print("Enter the second Number:"); num2 = scanner.nextInt(); System.out.print("Enter the third Number:"); num3 = scanner.nextInt(); scanner.close(); result = num3<(num1 < num2 ? num1 : num2) ? num3 : ((num1 < num2) ? num1 : num2); System.out.println("Smallest Number is:" + result); } } |
Output:
Enter the first Number:20
Enter the second Number:10
Enter the third Number:30
Smallest Number is:10