Max of 3 Using Nested If

Find the maximum of three numbers using nested if statements.

JavaBeginner
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");
        int a = sc.nextInt();
        System.out.print("Enter second number: ");
        int b = sc.nextInt();
        System.out.print("Enter third number: ");
        int c = sc.nextInt();

        int max;
        if (a >= b) {
            if (a >= c) {
                max = a;
            } else {
                max = c;
            }
        } else {
            if (b >= c) {
                max = b;
            } else {
                max = c;
            }
        }

        System.out.println("Maximum = " + max);

        sc.close();
    }
}

Output

Enter first number: 3
Enter second number: 9
Enter third number: 7
Maximum = 9

We use nested if inside another if to compare three numbers step by step.