Object-Oriented Programming with Java, part I – Exercise 17

This program gets the user to input two numbers and then outputs the greater number or says they are equal.

My Code

import java.util.Scanner;

public class GreaterNumber {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        // User input first number
        System.out.println("Type the first number: ");
        int firNum = Integer.parseInt(reader.nextLine());
        //User input second number
        System.out.println("Type the second number: ");
        int secNum = Integer.parseInt(reader.nextLine());
        // White Space
        System.out.println("");
        // Output result based on which is greater
        if (firNum > secNum){
            System.out.println("Greater number: " + firNum);
        } else if (firNum < secNum){
            System.out.println("Greater number: " + secNum);
        } else {
            System.out.println("The numbers are equal!");
        }

    }
}

Model Code

import java.util.Scanner;

 

public class GreaterNumber {

 

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

 

        System.out.print("Type the first number: ");

        int first = Integer.parseInt(reader.nextLine());

        System.out.print("Type the second number: ");

        int second = Integer.parseInt(reader.nextLine());

 

        if (first > second) {

            System.out.println("Greater number: " + first);

        } else if (second > first) {

            System.out.println("Greater number: " + second);

        } else {

            System.out.println("The numbers are equal!");

        }

    }

}

 
Leave a Reply

Your email address will not be published. Required fields are marked *