Java program to find the largest among three numbers can be computed by comparing them with each other. A number x is said to be the largest among three numbers when its value is greater than other two numbers say y and z. For example, 20 is the largest among three numbers 10, 15, 20.

## 1. Algorithm to find the largest among three numbers

// $x$, $y$, $z$ are three input numbers//

1. Check if $x$ is greater or equal to $y$

2. If step [1] is true, then check if $x$ is greater or equal to $z$

3. If step [2] is true, then print $x$ is the largest

4. If step [2] is false, then print $z$ is the largest

5. If step [1] is false, then check if $y$ is greater or equal to $z$

6. If step [5] is true, then print $y$ is the largest

7. If step [5] is false then print $z$ is the largest

## 2. Pseudocode to find the largest among three numbers

Input: Three numbers $x$, $y$, $z$

Output: Largest among $x$, $y$, $z$

1. Procedure findLargest($x$, $y$, $z$):

2. If $x \geq y$:

3. If $x \geq z$:

4. Return $x$

5. Else:

6. Return $z$

7. Else:

8. If $y \geq z$:

9. Return $y$

10. Else:

11. Return $z$

12. End Procedure

## 3. Time Complexity to find the largest among three numbers

Time Complexity: O(1)

## 4. Java Program to find the largest among three numbers

Code has been copied
/*****************************************************
alphabetacoder.com
Java program to find largest among three numbers
*****************************************************/

import java.util.Scanner;
public class Largest {
public static void main(String args[]) {
// declare variables
int x, y, z;

// declare an instance of scanner class
Scanner sc = new Scanner(System.in);

//take input of three numbers
System.out.print("Enter the numbers = ");
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();

// find the largest number
if (x >= y) {
if (x >= z)
System.out.print(x + " is the largest number.");
else
System.out.print(z + " is the largest number.");
} else {
if (y >= z)
System.out.print(y + " is the largest number.");
else
System.out.print(z + " is the largest number.");
}
}
}


Output

#### Case 1:

Enter the three numbers = -3 -8 -23

-3 is the largest number.

#### Case 2:

Enter the three numbers = 9 3 6

9 is the largest number.