🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Finding the largest of three numbers is a basic programming task often used to practice conditional statements and logical comparisons. This guide will show you how to create a Java program that takes three numbers as input and determines the largest among them.
Problem Statement
Create a Java program that:
- Takes three integer inputs from the user.
- Compares the three numbers.
- Returns and displays the largest number.
Example 1:
- Input:
3, 5, 7 - Output:
The largest number is 7
Example 2:
- Input:
10, 20, 15 - Output:
The largest number is 20
Example 3:
- Input:
25, 25, 25 - Output:
The largest number is 25(all are equal)
Solution Steps
- Prompt for Input: Use the
Scannerclass to read three integer inputs from the user. - Compare the Numbers Using Conditional Statements: Use
if-elseorternaryoperator to compare the three numbers and determine the largest. - Display the Result: Print the largest number.
Java Program
Approach 1: Using if-else Statements
import java.util.Scanner; /** * Java Program to Find the Largest of Three Numbers using if-else statements * Author: https://www.javaguides.net/ */ public class LargestOfThree { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Prompt the user for input 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(); // Step 2: Compare the numbers using if-else statements int largest; if (num1 >= num2 && num1 >= num3) { largest = num1; } else if (num2 >= num1 && num2 >= num3) { largest = num2; } else { largest = num3; } // Step 3: Display the result System.out.println("The largest number is " + largest); } } Explanation
Input: The program prompts the user to enter three numbers.
Comparison:
- The
if-elsestatements compare the three numbers to determine which one is the largest. - If
num1is greater than or equal to bothnum2andnum3, thennum1is the largest. - Otherwise, if
num2is greater than or equal to bothnum1andnum3, thennum2is the largest. - If neither
num1nornum2is the largest, thennum3is the largest.
- The
Output: The program prints the largest number.
Output Example
Example 1:
Enter the first number: 3 Enter the second number: 5 Enter the third number: 7 The largest number is 7 Example 2:
Enter the first number: 10 Enter the second number: 20 Enter the third number: 15 The largest number is 20 Example 3:
Enter the first number: 25 Enter the second number: 25 Enter the third number: 25 The largest number is 25 Approach 2: Using Ternary Operator
import java.util.Scanner; /** * Java Program to Find the Largest of Three Numbers using Ternary Operator * Author: https://www.javaguides.net/ */ public class LargestOfThreeTernary { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Prompt the user for input 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(); // Step 2: Compare the numbers using the ternary operator int largest = (num1 >= num2) ? (num1 >= num3 ? num1 : num3) : (num2 >= num3 ? num2 : num3); // Step 3: Display the result System.out.println("The largest number is " + largest); } } Explanation
- Comparison Using Ternary Operator:
- The ternary operator simplifies the comparison by nesting conditional checks:
- If
num1is greater than or equal tonum2, the program checks whethernum1is also greater than or equal tonum3. - If not, it compares
num2withnum3to find the largest.
- If
- The ternary operator simplifies the comparison by nesting conditional checks:
Output Example
The output for the ternary operator approach will be the same as the if-else approach.
Conclusion
This Java program demonstrates how to find the largest of three numbers using both if-else statements and the ternary operator. Both approaches are effective and straightforward, with the ternary operator offering a more concise syntax. This basic problem is a good exercise for practicing conditional statements and can be extended to more complex comparisons if needed.
Comments
Post a Comment
Leave Comment