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

Exercise 8

This program asks the user for two integers and then prints their sum.

My code

import java.util.Scanner;

public class Adder {

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

        // Implement your program here. Remember to ask the input from user
        System.out.println("Type a number: ");
        int num1 = Integer.parseInt(reader.nextLine());
        
        System.out.println("Type another number: ");
        int num2 = Integer.parseInt(reader.nextLine());
        
        System.out.println("Sum of the numbers: " + (num1 + num2));
    }
}

Model code

import java.util.Scanner;

 

public class Adder {

 

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

 

        // Implement your program here. Remember to ask the input from user

        System.out.print("Type a number: ");

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

        System.out.print("Type another number: ");

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

 

        System.out.println(""); // an empty line

        System.out.println("Sum of the numbers: " + (firstNumber + secondNumber));

    }

}
Leave a Reply

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