# Java program to find largest of four numbers using nested if

Let’s learn java program to find largest of four numbers using nested if.

## Java program to find largest of four numbers using nested if

In below java program first user is asked to enter four numbers one by one using nextInt() method of Scanner class.

These four numbers are stored in four integer variables number1, number2, number3 and number4. Then using nested if largest number is found among four numbers.

```import java.util.Scanner;
public class LargestOfFourNumbers
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int number1 = sc.nextInt();
int number2 = sc.nextInt();
int number3 = sc.nextInt();
int number4 = sc.nextInt();
if(number1 > number2)
{
if(number1 > number3)
{
if(number1 > number4)
{
System.out.println("Largest of four numbers is: " + number1);
}
else
{
System.out.println("Largest of four numbers is: " + number4);
}
}
}
else if(number2 > number3)
{
if(number2 > number4)
{
System.out.println("Largest of four numbers is: " + number2);
}
else
{
System.out.println("Largest of four numbers is: " + number4);
}
}
else if(number3 > number4)
{
System.out.println("Largest of four numbers is: " + number3);
}
else
{
System.out.println("Largest of four numbers is: " + number4);
}
sc.close();
}
}

```

Output: