Write a Java program for the largest of three numbers using Else If Statement, Nested If, and Conditional Operator. There are many approaches to find the largest number among three numbers in Java, but we are using these three.

## Java Program for Largest of Three Numbers using Else If Statement

This Java program helps the user to enter three different values and finds the largest number among that three numbers using the Else If Statement

package SimpleNumberPrograms; import java.util.Scanner; public class LargestUsingElseIf { private static Scanner sc; public static void main(String[] args) { int x, y, z; sc = new Scanner(System.in); System.out.println("\n Please Enter three Different Value: "); x = sc.nextInt(); y = sc.nextInt(); z = sc.nextInt(); if (x > y && x > z) { System.out.format("\n% d is Greater Than both %d and %d", x, y, z); } else if (y > x && y > z) { System.out.format("\n% d is Greater Than both %d and %d", y, x, z); } else if (z > x && z > y) { System.out.format("\n% d is Greater Than both %d and %d", z, x, y); } else { System.out.println("\n Either any two values or all the three values are equal"); } } }

Java Program for Largest of Three Numbers Analysis

- The first if condition within the Java Else If Statement check whether x is greater than y and x is greater than z. if the condition is True, then x is greater than both y, z.
- The first Else if condition checks whether y is greater than x and y is greater than z. if it is True, then y is greater than both x, z.
- The second Else if condition check whether z is greater than x and z is greater than y. if it is True, then z is greater than both x, y.
- If all the above conditions fail, it means they are equal.

OUTPUT 1: Lets enter the values x = 50, y = 75, z = 98

Lets enter the values x= 15, y= 5, z= 4

```
Please Enter three Different Value:
15
5
4
15 is Greater Than both 5 and 4
```

OUTPUT 3

```
Please Enter three Different Value:
15
60
4
60 is Greater Than both 15 and 4
```

OUTPUT4

```
Please Enter three Different Value:
15
15
11
Either any two values or all the three values are equal
```

## Java Program for Largest of Three Numbers using Nested If Statement

This Java program finds the largest number among that three numbers using Nested If.

package SimpleNumberPrograms; import java.util.Scanner; public class LargUsingNestedIf { private static Scanner sc; public static void main(String[] args) { int x, y, z; sc = new Scanner(System.in); System.out.println("\n Please Enter three Different Value: "); x = sc.nextInt(); y = sc.nextInt(); z = sc.nextInt(); LarOfTh(x, y, z); } public static void LarOfTh(int x, int y, int z) { if (x - y > 0 && x - z > 0) { System.out.format("\n% d is Greater Than both %d and %d", x, y, z); } else { if (y -z > 0) { System.out.format("\n% d is Greater Than both %d and %d", y, x, z); } else { System.out.format("\n% d is Greater Than both %d and %d", z, x, y); } } } }

Java Program for Largest of Three Numbers Analysis

- The first Java if condition check whether (x – y) is greater than 0 and (x – z) is greater than 0. If we subtract a small number from a big number, then this condition fails. Otherwise, it will be True. If this condition is True, then x is greater than both y, z.
- Else statement will execute when the first if condition is False. So, there is no need to check for x value. In the Else statement, we are inserting one more if condition (Nested IF) to check whether (y – z) is greater than 0. If it is True, then y is greater than both x, z.
- Else z is greater than both x, y.

OUTPUT: Lets enter the values x = 15, y = 87, z = 26

```
Please Enter three Different Value:
15
87
26
87 is Greater Than both 15 and 26
```

## Java Program for Largest of Three Numbers using Ternary Operator

This Java program finds the largest number among that three numbers using Ternary Operator

package SimpleNumberPrograms; import java.util.Scanner; public class LagUsingConditionalOper { private static Scanner sc; public static void main(String[] args) { int x, y, z, lar; sc = new Scanner(System.in); System.out.println("\n Please Enter three Different Value: "); x = sc.nextInt(); y = sc.nextInt(); z = sc.nextInt(); lar = ((x > y && x > z)? x: (y > z)?y:z); System.out.format("\n Largest number among three is: %d", lar); } }

Java Program for Largest of Three Numbers Analysis

- In this Java Program for Largest of Three Numbers example, the first condition checks whether x is greater than y and x is greater than z. If this condition is True, then it will return the first value after the ? symbol, which is variable x (x is greater than both y, z).
- If the first condition fails, then it will execute the variable after the : symbol. By using the Nested conditional operator we are checking one more condition here (y > z). If this condition is True, then it will return the first value after the ? symbol, which is variable y (y is greater than both x, z.
- If the Nested condition fails, then it will execute the variable after the : symbol, which is variable z. It means z is greater than both x, y.

OUTPUT 1: Lets enter the values x= 15, y= 6, z= 9

```
Please Enter three Different Value:
15
6
9
Largest number among three is: 15
```

Lets enter the different values

```
Please Enter three Different Value:
5
98
87
Largest number among three is: 98
```

OUTPUT 3:

```
Please Enter three Different Value:
100
500
980
Largest number among three is: 980
```

## Java Program for Largest of Three Numbers using Oops

In this example, we are dividing the Java Program for the Largest of Three Numbers code using Object-Oriented Programming. To do this, we will create a class that holds two methods to find the largest of two numbers.

package SimpleNumberPrograms; public class LargestNumber { int x, y, z, largest; public void LOT() { if (x - y > 0 && x - z > 0) { System.out.format("\n% d is Greater Than both %d and %d", x, y, z); } else { if (y -z > 0) { System.out.format("\n% d is Greater Than both %d and %d", y, x, z); } else { System.out.format("\n% d is Greater Than both %d and %d", z, x, y); } } } public int LOTNumber(int x, int y, int z) { largest = ((x > y && x > z)? x: (y > z)?y:z); return largest; } }

Within the Main Java Program for Largest of Three Numbers, we will create an instance of the above class and call the methods

package SimpleNumberPrograms; import java.util.Scanner; public class LargestUsingClass { private static Scanner sc; public static void main(String[] args) { int a, b, c, largest; sc = new Scanner(System.in); System.out.println("\n Please Enter three Different Value: "); a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt(); LargestNumber ln = new LargestNumber(); ln.x = a; ln.y = b; ln.z = c; ln.LOT(); largest = ln.LOTNumber(a, b, c); System.out.format("\n Largest number among three is = %d", largest); } }

```
Please Enter three Different Value:
45
75
25
75 is Greater Than both 45 and 25
Largest number among three is: 75
```

LargestNumber Class Analysis:

- First, we declared a function LOT, with zero arguments. Within the function, we used the Nested If statement to find the largest of the three numbers. Within the function, we used System.out.println statements to print the output as well.
- Next, we declared an integer function LOTNumbers, with three arguments. We already explained the Logic in the above example.

Java Program for Largest of Three Numbers Main Class Analysis:

Within this Java Program for Largest of Three Numbers, we created an instance/created an Object of the LargestNumber Class

LargestNumber ln = new LargestNumber();

Next, we are assigning the user entered values to the LargestNumber Class variables.

ln.x = a; ln.y = b; ln.z = c;

Next, we are calling the LOT method. Note, this is the first method that we created with the void keyword, and it will find the largest of three numbers and print the output from the LargestNumber Class itself.

ln.LOT();

Next, we are calling the LOTNumbers method by passing three arguments. This function will find the largest of three numbers and return the largest value. That is why we are assigning the output to the largest variable.

largest = ln.LOTNumber(a, b, c);

Lastly, System.out.println statement will print the output.