In Java, finding the largest of three numbers involves comparing the values of three given numbers and identifying the maximum among them. There are multiple approaches to solve this problem, including using conditional statements, the Math.max() function, or sorting the numbers. Let’s explore a few examples to better understand how to find the largest of three numbers in Java.
Approach 1: Using Ternary Operator
//Largest of Three Numbers in java import java.util.Scanner; public class LargestNumEx { public static void main(String[] args) { int x, y, z, largest, temp; //object of the Scanner class Scanner sc = new Scanner(System.in); //reading input from the user System.out.println("Enter the first number:"); x = sc.nextInt(); System.out.println("Enter the second number:"); y = sc.nextInt(); System.out.println("Enter the third number:"); z = sc.nextInt(); //comparing a and b and storing the largest number in a temp variable temp=x>y?x:y; //comparing the temp variable with c and storing the result in the variable largest=z>temp?z:temp; //prints the largest number System.out.println("The largest number is: "+largest); } }
Explanation of the code:
The given code is a Java program that finds the largest of three numbers entered by the user. Here’s a breakdown of the code:
1. Importing Required Libraries:
import java.util.Scanner;
The code imports the Scanner class from the java.util package. Additionally, the Scanner class allows us to read input from the user.
2. Creating the Main Class:
public class LargestNumEx { public static void main(String[] args) {
The code defines a public class named “LargestNumEx” and declares the main method, which is the entry point of the program.
3. Variable Declaration and User Input:
int x, y, z, largest, temp; Scanner sc = new Scanner(System.in); System.out.println("Enter the first number:"); x = sc.nextInt(); System.out.println("Enter the second number:"); y = sc.nextInt(); System.out.println("Enter the third number:"); z = sc.nextInt();
The code declares variables for the three numbers (x, y, and z), as well as additional variables for the largest number (largest) and a temporary variable (temp). It creates a Scanner object (sc) to read input from the user. The user is prompted to enter three numbers, which are then stored in the corresponding variables.
4. Finding the Largest Number:
temp = x > y ? x : y; largest = z > temp ? z : temp;
The code uses the conditional (ternary) operator to compare the first two numbers (x and y). It assigns the larger of the two to the temporary variable (temp). Then, it compares the third number (z) with the value stored in the temporary variable (temp) and assigns the larger value to the variable “largest”.
5. Displaying the Result:
System.out.println("The largest number is: " + largest); ``` The code prints the result by displaying the largest number to the console. 6. Closing the Main Method and Class: ```java } }
The code closes the main method and the LargestNumEx class.
In summary, this Java program takes three numbers as input from the user, compares them using conditional operators, and determines the largest among them. It then displays the largest number to the console.
Output:
Enter the first number:
45
Enter the second number:
62
Enter the third number:
554
The largest number is: 554
Learn How to Palprime Number in Java, Here!
Approach 2: Using if-else..if
//Largest of Three Numbers in java import java.util.Scanner; public class LargestNumEx3 { public static void main(String[] args) { //initializing numbers to compare int a, b, c; //object of the Scanner class Scanner sc = new Scanner(System.in); //reading input from the user 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(); //comparing numbers, a with b and a with c //if both conditions are true, prints a if(a>=b && a>=c) System.out.println(a+" is the largest Number"); //comparing b with a and b with c //if both conditions are true, prints b else if (b>=a && b>=c) System.out.println(b+" is the largest Number"); else //prints c if the above conditions are false System.out.println(c+" is the largest number"); } }
Also, learn about Swap of Two Numbers in Java, Now!
Explanation of the code:
Additionally, the provided code is a Java program that determines the largest of three numbers entered by the user. Here’s a summary of the code:
1. Variable Declaration and User Input:
The program declares three variables (a, b, and c) to store the input numbers. In addition it utilizes the Scanner class to read the user’s input.
2. Comparing the Numbers:
The program compares the three numbers using a series of if-else statements. Firstly, it compares the value of a with b and c. If a is greater than or equal to both b and c, it prints that a is the largest number. If not, subsequently, it moves to the next condition.
3. Further Comparisons:
If the previous condition is false, the program compares the value of b with a and c. Else if b is greater than or equal to both a and c, it prints that b is the largest number. If none of the previous conditions are satisfied, consequently, it concludes that c is the largest number.
4. Displaying the Result:
Finally the program prints the result by displaying the largest number to the console.
In summary, this Java program takes three numbers as input, compares them using if-else statements, and determines the largest among them. It then displays the largest number as the output.
Output:
Enter the first number:
85
Enter the second number:
54
Enter the third number:
23
85 is the largest Number
Get complete Java Programming Exercises and Solutions here!
Approach 3: Using nested if
//Largest of Three Numbers in java import java.util.Scanner; public class LargestNumEx4 { public static void main(String[] args) { //initializing numbers to compare int x, y, z; //object of the Scanner class Scanner sc = new Scanner(System.in); //reading input from the user System.out.println("Enter the first number:"); x = sc.nextInt(); System.out.println("Enter the second number:"); y = sc.nextInt(); System.out.println("Enter the third number:"); z = sc.nextInt(); if(x >= y) { if(x >= z) //prints x, if the above two conditions are true System.out.println("The largest number is: "+x); else //prints z, if the condition defined in inner if is true and the condition defined in inner if is false means x>y and x<z System.out.println("The largest number is: "+z); } else { if(y >= z) //prints y, if the condition defined in outer if is false and the condition defined in inner if is tr means z>x and y>z System.out.println("The largest number is: "+y); else //prints z, if the condition defined in both inner and outer loop is false z>x and z>y System.out.println("The largest number is: "+z); } } }
Explanation of the code:
The provided code is a Java program that determines the largest of three numbers entered by the user. Here’s a summary of the code:
1. Variable Declaration and User Input:
The program declares three variables (x, y, and z) to store the input numbers. Additionally, it uses the Scanner class to read the user’s input.
2. Comparing the Numbers:
The program uses nested if-else statements to compare the numbers. It first compares x with y. If x is greater than or equal to y, it moves to the inner if-else statement. Within the inner statement, it compares x with z. If x is greater than or equal to z, it prints that x is the largest number. If not, it concludes that z is the largest number.
3. Further Comparisons:
If the initial comparison of x and y is false, the program moves to the outer else statement. Here, it compares y with z. If y is greater than or equal to z, it prints that y is the largest number. If the condition in the outer else statement is false, it concludes that z is the largest number.
4. Displaying the Result:
Finally the program prints the result by displaying the largest number to the console.
In summary, this Java program takes three numbers as input, compares them using nested if-else statements, and determines the largest among them. It then displays the largest number as the output.
Output:
Enter the first number:
12
Enter the second number:
65
Enter the third number:
45
The largest number is: 65
Reasons why finding the largest of three numbers in Java is Important:
Here are a few reasons why finding the largest of three numbers in Java is essential:
1. Decision-making and Control Flow:
In programming, decision-making is a fundamental aspect. By finding the largest of three numbers, moreover, you can make informed decisions and control the flow of your program based on certain conditions. It allows you to direct the program’s execution path and perform specific actions based on the comparison results.
2. Algorithm Design and Problem Solving:
Finding the largest of three numbers is often a building block in more complex algorithms and moreover in problem-solving scenarios. Many programming challenges and mathematical calculations require the identification of the maximum value among multiple inputs. Furthermore, understanding this concept equips you with the skills to approach more intricate problems effectively.
3. Data Analysis and Statistics:
In data analysis and statistics, determining the largest value is crucial for various operations. Whether you’re working with financial data, analyzing sales figures, or examining survey responses, identifying the maximum value helps extract meaningful insights. It allows you to understand trends, outliers, and critical data points that influence decision-making in various domains.
4. Sorting and Ranking:
Sorting elements in ascending or descending order is a common task in programming. When dealing with a collection of numbers, knowing the largest value is essential for proper sorting and ranking. Whether you’re arranging a list of scores, identifying the highest bidder, or ordering items by size, finding the largest value is a fundamental step.
5. Optimization and Resource Allocation:
In certain scenarios, the largest value represents a significant resource or determines optimal allocation. For example, in resource management systems, identifying the largest value could mean allocating the most computing power to a particular task or assigning the largest budget to a critical project. By finding the maximum value, you can optimize resource utilization and enhance efficiency.
6. Mathematical Calculations:
In mathematical calculations and formulas, determining the largest value among several inputs is often necessary. Whether you’re calculating averages, determining ranges, or performing complex calculations involving multiple variables, knowing the largest value helps ensure accurate and meaningful results.
In conclusion, the ability to find the largest of three numbers in Java is a fundamental skill with wide-ranging applications. From decision-making and algorithm design to data analysis and resource allocation, knowing the largest value among multiple inputs is essential. By mastering this concept, programmers can effectively solve problems, make informed choices, furthermore, extract valuable insights from data, contributing to the development of robust and efficient applications.
We hope that our blog post on “Largest of Three Numbers in Java” will answer any queries you may have about Java. As you continue to develop your coding skills, visit the Newtum‘s website to learn more about our online coding courses in Java, Python, PHP, and other topics. With practice and dedication, you can indeed master Java development and acquire new programming concepts.