
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Largest of Three Numbers Using Ternary Operators in Java
In this article, we'll learn to find the largest of three numbers using the ternary operator in Java. This simple yet effective approach allows you to write concise and readable code. Finding the largest of three numbers is a common programming task, and Java provides an efficient way to do this using the ternary operator. This concise approach reduces code complexity while maintaining readability.
What is the Ternary Operator in Java?
The ternary operator is a shorthand for if-else statements in Java. It uses the syntax:
condition ? value_if_true : value_if_false
This operator is useful for simple conditional checks and can significantly reduce the lines of code required for basic decision-making logic.
Program to Find the Largest of Three Numbers
The program uses a Scanner object of the Java Scanner class to accept three numbers from the user.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
Finding the largest number using nested ternary operators
(num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);
First, it checks if num1 is greater than num2. If true, it further checks if num1 is greater than num3 and assigns the larger of the two to the largest. If false, it checks if num2 is greater than num3 and assigns the larger of the two to the largest.
Example
Here's a complete Java program to determine the largest of three numbers using the ternary operator:
import java.util.Scanner; public class LargestOfThree { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input three numbers from the user System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); System.out.print("Enter the third number: "); int num3 = scanner.nextInt(); // Find the largest number using nested ternary operators int largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3); // Display the largest number System.out.println("The largest number is: " + largest); scanner.close(); } }
Output
The largest number is: 67
Benefits of Using the Ternary Operator
-
Conciseness: Reduces the number of lines of code compared to if-else statements.
-
Readability: Makes simple conditional logic easier to follow.
- Efficiency: Ideal for quick decision-making logic in small programs.
Conclusion
Finding the largest of three numbers using the ternary operator in Java is a straightforward and elegant solution. This program is an excellent example of how to apply Java's core concepts to solve real-world problems.