Solution - Find the Larger Number

Problem.

Write a Java program that asks the user to input two integers and then prints the larger of the two numbers. If the two numbers are equal, print a message indicating that they are the same.

Solution.

FindLargerNumber.java

import java.util.Scanner;

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

        System.out.print("Enter the first number: ");
        int num1 = input.nextInt();

        System.out.print("Enter the second number: ");
        int num2 = input.nextInt();

        if (num1 == num2) {
            System.out.println("The two numbers are the same.");
        } else {
            if (num1 > num2) {
                System.out.println("The larger number is " + num1 + ".");
            } else {
                System.out.println("The larger number is " + num2 + ".");
            }
        }

        input.close();
    }
}