import java.util.Scanner;
class LargestOfThreeNumbers {
public static void main(String args[]) {
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
if (x > y && x > z)
System.out.println("First number is largest.");
else if (y > x && y > z) System.out.println("Second number is largest.");
else if (z > x && z > y) System.out.println("Third number is largest.");
else
System.out.println("Entered numbers are not distinct.");
}
}
Let’s go line by line through the Java program, LargestOfThreeNumbers, to see how it works, then demonstrate the output.
Step-by-Step Explanation:
1. Import the class of Scanner type:
import java.util.Scanner;
The Scanner class is imported from the java.util package, allowing us to take user input from the console.
2. Class LargestOfThreeNumbers:
class LargestOfThreeNumbers {
The class contains the logic for determining the largest number of three integers.
3. Method main:
public static void main(String args[]) {
This is the main method of a Java program. This is where the program starts its execution.
4. Declare integer variables
int x, y, z;
Declare three integer variables x, y and z to hold three integers which will be read by the user from the console.
5. Prompt the user to enter three integers
System.out.println(“Enter three integers”);
This statement prints the string “Enter three integers ” to the console, indicating that the program is asking for three numbers as input.
6. Declaration of a Scanner object to accept input:
Scanner in = new Scanner(System.in);
A Scanner named in is declared, which reads user input from the console (standard input).
7. Scan for user inputs for the three integers:
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
These three lines of code use the nextInt() method to input three integer values from the keyboard and store these values in variables x, y, and z.
8. Identify the biggest of the three numbers:
The program uses a series of if-else statements to compare the values of x, y, and z to determine which one is the largest:
if (x > y && x > z)
System.out.println(“First number is largest.”);
This is going to check if x is greater than both of the variables y and z. If this is the case, then it will print “First number is largest.”.
else if (y > x && y > z)
System.out.println(“Second number is largest.”);
If the first condition is false, it then checks if y is larger than both x and z. In case that happens, it will print out “Second number is largest.”
else if (z > x && z > y)
System.out.println(“Third number is largest.”);
If both previous conditions are false, it checks if z is greater than both x and y. If true, it prints “Third number is largest”.
9. Handle the case where numbers are not distinct:
else
System.out.println(“Entered numbers are not distinct.”);
If none of the above conditions are met, it implies at least two numbers are equal. It then outputs “Entered numbers are not distinct.”
Example Execution:
Example 1: Distinct Numbers
Input:
Enter three integers
10
20
30
In this case, x = 10, y = 20, and z = 30.
It then checks
- Are x > y and x > z? No (because 10 is not greater than 20 or 30).
- Are y > x and y > z? No (because 20 is not greater than 30).
- Are z > x and z > y? Yes (because 30 is greater than both 10 and 20).
Output:
Third number is largest.
Example 2: First Number is the Largest
Input:
Enter three integers
40
20
10
Here, x = 40, y = 20, and z = 10.
The program will check: Is x > y and x > z? Yes (40 is greater than both 20 and 10).
Output:
First number is largest.
Example 3: Numbers Are Equal
Input:
Enter three integers
10
10
10
Here, x = 10, y = 10, and z = 10.
The program will check: All the conditions to find the largest number are false because all the numbers are equal.
Output:
Entered numbers are not distinct.
Summary of Output:
1. For input 10, 20, 30:
Third number is largest.
2. For input 40, 20, 10:
First number is largest.
3. For input 10, 10, 10:
Entered numbers are not distinct.
Conclusion:
- The program successfully determines which of the three numbers is the largest using if-else conditions.
- If two or more numbers are equal, the program informs the user that the numbers are not distinct.
- The program handles both distinct and non-distinct numbers.