w3resource

Java: Compare two numbers

Java Basic: Exercise-32 with Solution

Write a Java program to compare two numbers.

Test Data:
Input first integer: 25
Input second integer: 39

Pictorial Presentation:

Java: Compare two numbers

Sample Solution:

Java Code:

import java.util.Scanner;

public class Exercise32 {
    public static void main(String args[]) {
        // Create a Scanner to obtain input from the command window
        Scanner input = new Scanner(System.in);
        int number1; // First number to compare
        int number2; // Second number to compare

        // Prompt the user to input the first integer
        System.out.print("Input first integer: ");
        number1 = input.nextInt(); // Read the first number from the user

        // Prompt the user to input the second integer
        System.out.print("Input second integer: ");
        number2 = input.nextInt(); // Read the second number from the user

        // Compare and display the results
        if (number1 == number2)
            System.out.printf("%d == %d\n", number1, number2);
        if (number1 != number2)
            System.out.printf("%d != %d\n", number1, number2);
        if (number1 < number2)
            System.out.printf("%d < %d\n", number1, number2);
        if (number1 > number2)
            System.out.printf("%d > %d\n", number1, number2);
        if (number1 <= number2)
            System.out.printf("%d <= %d\n", number1, number2);
        if (number1 >= number2)
            System.out.printf("%d >= %d\n", number1, number2);
    }
}
  

Explanation:

In the exercise above –

  • First, it uses the "Scanner" class to obtain input from the command window (user input).
  • It declares two integer variables 'number1' and 'number2' to store the two numbers provided by the user.
  • It prompts the user to input the first integer using System.out.print("Input first integer: ") and reads the input into 'number1'.
  • It prompts the user to input the second integer using System.out.print("Input second integer: ") and reads the input into 'number2'.
  • Next it performs a series of comparisons using if statements and prints the results using System.out.printf():
    • if (number1 == number2) checks if 'number1' is equal to 'number2' and prints a message if they are equal.
    • if (number1 != number2) checks if 'number1' is not equal to 'number2' and prints a message if they are not equal.
    • if (number1 < number2) checks if 'number1' is less than 'number2' and prints a message if 'number1' is less than 'number2'.
    • if (number1 > number2) checks if 'number1' is greater than 'number2' and prints a message if 'number1' is greater than 'number2'.
    • if (number1 <= number2) checks if 'number1' is less than or equal to 'number2' and prints a message accordingly.
    • if (number1 >= number2) checks if 'number1' is greater than or equal to number2 and prints a message accordingly.

Sample Output:

Input first integer: 25                                                                                       
Input second integer: 39                                                                                      
25 != 39                                                                                                      
25 < 39                                                                                                       
25 <= 39 

Flowchart:

Flowchart: Java exercises: Compare two numbers

Java Code Editor:

Previous: Write a Java program to check whether Java is installed on your computer.
Next: Write a Java program and compute the sum of the digits of an integer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.