Write a Java program for largest of three numbers using Else If Statement, Nested If and Conditional Operator. Although We have many approaches to find largest number among three numbers.

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

This Java program helps the user to enter three different values and then it will find the largest number among that three numbers using Java 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"); } } }

**ANALYSIS**

- First if condition 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.
- First Else if condition check whether y is greater than x and y is greater than z. if this condition is True then y is greater than both x, z.
- Second Else if condition check whether z is greater than x and z is greater than y. if this condition 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

**OUTPUT 2: **Lets enter the values x = 15, y = 5, z = 4

**OUTPUT 3: **Lets enter the values x = 15, y = 60, z = 4

**OUTPUT 4: **Lets enter the values x = 15, y = 15, z = 4

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

This Java program helps the user to enter three different values and then it will find 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); } } } }

**ANALYSIS**

- First if condition check whether (x – y) is greater than 0 and (x – z) is greater than 0. If we subtract small number from big number then this condition fail, 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 this condition 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

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

This Java program helps the user to enter three different values and then it will find 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); } }

**ANALYSIS**

- In this Java Program for Largest of Three Numbers example, We declared an integer variable called largest and we assigned this variable to the conditional operator functionality.
- First condition check whether x is greater than y and x is greater than z. If this condition is True then it will return first value after the
**?**symbol, which is variable x (x is greater than both y, z). - If the first condition fail then it will execute the variable after the
**:**symbol. By using Nested conditional operator we are checking one more condition here (y > z). If this condition is True then it will return 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

**OUTPUT 2: **Lets enter the values x = 5, y = 98, z = 87

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

## Java Program for Largest of Three Numbers using Oops

In this example, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds two methods to find 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 program, we will create an instance of the above specified 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); } }

**OUTPUT**

**ANALYSIS**

**LargestNumber Class Analysis:**

- First we declared a function
**LargestOfThree**with zero arguments. Within the function, we used Nested If statement to find 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 above example.

**TIP:** If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

**Main Class Analysis:**

Within this Java Program for Largest of Three Numbers, First 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 void keyword and this method will find 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 the find largest of three numbers and return the largest value that’s why we are assigning the output to **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);