This program is similar to the previous one, with a single modification, here we will use if else statement to find the largest number.

Logic

The logic to find the largest number among the three numbers, we have to compare each number with the other two numbers.

Let three variables be: A = 400, B = 200 and C = 300

  1. if A > B and A > C, then print A.
  2. else if B > A and B > C, then print B.
  3. else print C.

Program

import java.util.Scanner;

public class IfElseStatement {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter 1st number : ");
        int num1 = scanner.nextInt();

        System.out.println("Enter 2nd number : ");
        int num2 = scanner.nextInt();

        System.out.println("Enter 3rd number : ");
        int num3 = scanner.nextInt();

        if(num1 >= num2 && num1 >= num3) {

             System.out.println(num1 + " is largest number");

        } else if(num2 >= num3) {
            
             System.out.println(num2 + " is largest number");

        } else {

             System.out.println(num3 + " is largest number");
        }

    }
}

Output

Enter 1st number : 10
Enter 2nd number : 200
Enter 3rd number : 11
200 is largest number