Largest of Two Numbers

Find the largest of two numbers using if-else.

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: ");
        double a = sc.nextDouble();

        System.out.print("Enter second number: ");
        double b = sc.nextDouble();

        if (a > b) {
            System.out.println("Largest = " + a);
        } else if (b > a) {
            System.out.println("Largest = " + b);
        } else {
            System.out.println("Both numbers are equal");
        }

        sc.close();
    }
}

Output

Enter first number: 5
Enter second number: 9
Largest = 9.0

We compare the two numbers using > and handle the equal case separately.