In this tutorial we will **write a program to find largest of three numbers in java** and we will also see **algorithm to find greatest of three numbers in java** . We can find **largest of three numbers in java **using if else , if else ladder , if else nested and ternary operator . Let’s see different java program for biggest of three numbers .

## Example 1 – Find Largest of Three Numbers In Java using If-Else

In this example, we will write java program to find largest of three numbers using if else . Firstly we will see algorithm to find greatest of three numbers .

**Algorithm for Largest of Three Numbers using if-else : **

- Take input from user for three numbers and store in variables a, b, c.
- Now check if variable a is greater than b.
- If above condition is true,then go to step 4, else go to step 6.
- Now check if a is greater than c.
- If above condition is true, then a is the largest, else c is the largest. Go to step 8.
- Now check if b is greater than c.
- If above condition is true,then b is the largest, else c is the largest.
- Print value of largest variable .

Now we will implement above algorithm and write **java program to find largest of three numbers** . We take input from user using scanner class .

```
import java.util.Scanner;
public class LargestOfThreeNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number : ");
int a = scanner.nextInt();
System.out.println("Enter second number : ");
int b = scanner.nextInt();
System.out.println("Enter third number : ");
int c = scanner.nextInt();
int largest;
// find the largest using if else
if (a > b) {
if (a > c) {
largest = a;
} else {
largest = c;
}
} else {
if (b > c) {
largest = b;
} else {
largest = c;
}
}
//print largest number
System.out.println(largest + " is the largest.");
//close scanner
scanner.close();
}
}
```

Output:

```
Enter first number :
10
Enter second number :
20
Enter third number :
15
20 is the largest.
```

## Example 2 – Find Largest of Three Numbers In Java using If-Else-If Ladder

Now we check greatest of three numbers in java using if-ele-if ladder . Firstly we discuss algorithm for largest of three numbers in java .

**Algorithm to find greatest of three numbers using if-else-if ladder :**

- Take input from user and store in variables in a, b, c.
- Now check if a is greater than b and a is greater than c.
- If above condition is true,then a is largest and go to step 6, else go to step 4.
- Now check if b is greater than c.
- If above condition is true,then b is the largest, else c is the largest.
- Print value of largest variable.

We will implement this above algorithm to find largest of three numbers in java.

```
import java.util.Scanner;
public class LargestOfThreeNumbers2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number : ");
int a = scanner.nextInt();
System.out.println("Enter second number : ");
int b = scanner.nextInt();
System.out.println("Enter third number : ");
int c = scanner.nextInt();
int largest;
// find the largest using if-else-if ladder
if(a>b && a>c) {
largest = a;
} else if (b>c) {
largest = b;
} else {
largest = c;
}
//print largest number
System.out.println(largest + " is the largest.");
//close scanner
scanner.close();
}
}
```

Output :

```
Enter first number :
50
Enter second number :
10
Enter third number :
30
50 is the largest.
```

## Example 3 – Java Program to Find Largest of Three Numbers Using nested if else

In this example we will check greatest of three numbers in java using nested if else . Firstly we see algorithm for largest of three numbers in java .

**Algorithm to find greatest of three numbers using nested if else :**

- Take input from user and store in variables in a, b, c.
- Now check if a is greater than b .
- If above condition is true,then go to step 4, else go to step 6.
- Now check if a is greater than c.
- If above condition is true,then a is the largest, else c is the largest and go to step 8.
- Check if b is greater than c.
- If above condition is true,then b is the largest, else c is the largest.
- Print value of largest variable.

Now write java program to find largest of three numbers using above algorithms .

```
import java.util.Scanner;
public class LargestOfThreeNumbers3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number : ");
int a = scanner.nextInt();
System.out.println("Enter second number : ");
int b = scanner.nextInt();
System.out.println("Enter third number : ");
int c = scanner.nextInt();
int largest;
//find the largest using nested if else
if (a > b) {
if(a > c) {
largest=a;
} else {
largest=c;
}
} else if ( b > c) {
largest=b;
} else {
largest=c;
}
//print largest number
System.out.println(largest + " is the largest.");
//close scanner
scanner.close();
}
}
```

Output:

```
Enter first number :
100
Enter second number :
50
Enter third number :
20
100 is the largest.
```

## Example 4 – Biggest of Three numbers using Ternary Operator in Java

We will find biggest of 3 numbers using nested ternary operator in java . We will take input from user using scanner class and store in three variables a , b, c . Now will check greatest of three numbers in java using ternary operator .

```
import java.util.Scanner;
public class LargestOfThreeNumbers4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number : ");
int a = scanner.nextInt();
System.out.println("Enter second number : ");
int b = scanner.nextInt();
System.out.println("Enter third number : ");
int c = scanner.nextInt();
//find the largest using nested ternary operator
int largest = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
//print largest number
System.out.println(largest + " is the largest.");
//close scanner
scanner.close();
}
}
```

Output :

```
Enter first number :
60
Enter second number :
100
Enter third number :
90
100 is the largest.
```

In this tutorial we learned how to find largest of three numbers in java . We have used different method to check greatest of three numbers in java. you can practice more java program for interview .