Compare Two Integers Numbers Using Java

(Comparing Integers) Write an application that asks the user to enter two integers, obtains them from the user and displays the larger number followed by the words “is larger”. If the numbers are equal, print the message “These numbers are equal”.

Comparing two Integers in java program

public class ComparingIntegers
	// main method begins execution of Java application
	public static void main(String[] args) 
		// create Scanner to obtain input from command window
		Scanner input = new Scanner(;
		// initialize variables
		int num1;	// first integer for input
		int num2;	// second integer for input
		System.out.print("Enter first integer: ");	// prompt
		num1 = input.nextInt();	// read first integer from user
		System.out.print("Enter second integer: ");	// prompt
		num2 = input.nextInt();	// read second integer from user
		if (num1 > num2)
			System.out.printf("%d is larger than %d\n",num1,num2);
		if (num1 < num2)
			System.out.printf("%d is larger than %d\n", num2, num1);
		if (num1 == num2)
			System.out.println("These numbers are equal");

	}	// end method main

}	// end class ComparingIntegers

/* Output:

  • Enter first integer: 10
  • Enter second integer: 4
  • 10 is larger than 4
    • Enter first integer: 0
  • Enter second integer: 9
  • 9 is larger than 0
    • Enter first integer: 13
  • Enter second integer: 13
  • These numbers are equal

  • */

Comparing Integers in java program

import java.util.Scanner;

public class ComparingIntegers{
    public static void main(String[] args){
        Scanner input = new Scanner(;

        System.out.print("Enter 2 space separated integers: ");
        int x = input.nextInt();
        int y = input.nextInt();

        if(x != y)
            System.out.printf("%d is larger than %d\n", Math.max(x, y), Math.min(x,y));
            System.out.printf("%d and %d are equal\n", x, y);