# Java Program for Largest of Three Numbers

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. Next, this Java program will find the largest number among that three numbers using Else If Statement

```// Java Program for Largest of Three Numbers 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

1. 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.
2. 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.
3. 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.
4. 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: Lets enter the values x = 15, y = 60, z = 4

`````` Please Enter three Different Value:
15
60
4

60 is Greater Than both 15 and 4``````

Lets enter the values x = 15, y = 15, z = 11

`````` 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 in Java.

```// Java Program for Largest of Three Numbers using Nested If Statement
package SimpleNumberPrograms;

import java.util.Scanner;

public class LargestUsingNestedIf {
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();

LargestofThree(x, y, z);
}
public static void LargestofThree(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

1. 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.
2. 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.
3. 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 Java Ternary Operator

```// Java Program for Largest of Three Numbers using Conditional Operator

package SimpleNumberPrograms;

import java.util.Scanner;

public class LargestUsingConditionalOper {
private static Scanner sc;

public static void main(String[] args) {
int x, y, z, largest;
sc = new Scanner(System.in);
System.out.println("\n Please Enter three Different Value: ");
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();

largest = ((x > y && x > z)? x: (y > z)?y:z);
System.out.format("\n Largest number among three is: %d", largest);
}
}```

Java Program for Largest of Three Numbers Analysis

1. 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).
2. 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.
3. 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 values x = 5, y = 98, z = 87

`````` Please Enter three Different Value:
5
98
87

Largest number among three is: 98``````

OUTPUT 3: Lets enter the values x = 100, y = 500, z = 980

`````` 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 Largest of Three Numbers code using the Object-Oriented Programming. To do this, we will create a Java class that holds two methods to find the largest of two numbers.

```package SimpleNumberPrograms;

public class LargestNumber {
int x, y, z, largest;

public void LargestofThree() {
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 LargestofThreeNumber(int x, int y, int z) {
largest = ((x > y && x > z)? x: (y > z)?y:z);
return largest;
}
}```

Within the Main largest of three numbers program, 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.LargestofThree();

largest = ln.LargestofThreeNumber(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 LargestOfThree, with zero arguments. Within the function, we used Nested If statement to find the largest of three numbers. Within the function, we used System.out.println statements to print the output as well.
• Next, we declared an integer function LargestOfThreeNumbers, with three arguments. We already explained the Logic in the above example.

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 LargestOfThree 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.LargestofThree();`

Next, we are calling the LargestOfThreeNumbers 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.LargestofThreeNumber(a, b, c);`

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

`System.out.format("\n Largest number among three is = %d", largest);`