Thursday, August 28, 2014


Application to Compare Two Numbers Using Java

The program below is the answer to Deitel's Java How to Program (9th Edition) Chapter 2 Exercise 2.16.
Question: 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".

 * Website:

import java.util.Scanner;

public class ComparingIntegers {
    public static void main (String [] args) {
        Scanner value = new Scanner (;
        int num1; //first number entered by the user
        int num2; //second number entered by the user
        System.out.println ("Enter Your First Number");
        num1 = value.nextInt ();
        System.out.println ("Enter Your Second Number");
        num2 = value.nextInt ();
        System.out.println ();
        if (num1>num2) 
            System.out.println (num1 + " is Larger\n");
        if (num2>num1)
            System.out.println (num2 + " is Larger.\n");
        if (num1 == num2)
            System.out.println ("These numbers are equal.\n");

Click here to see other answers to Java How to Program.


  1. Replies
    1. Yes you can use else if but the instruction in the textbook says "use programming techniques you have learned so far". That means, even though the problem can be solved using another and probably better method, do not use it unless it has been taught in the previous or present chapter.
      In this case, the else if selection statement have not been taught in chapter two of the textbook.


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.