Simple calculator using Scanner object

 Below java class will explain you how to use the Scanner object which is a part of java.util package. This class is basically for integer values only however you can use the same way for double, float etc.


import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter your first integer value: ");
        int first = in.nextInt();
        System.out.println("Enter your second integer value: ");
        int second = in.nextInt();

        int plus = first + second;
        int minus = first - second;
        int multiply = first * second;
        int divide = first / second;

        System.out.println("Summation for " + first + " and " + second
                + " is: \t\t" + plus);
        System.out.println("Subtraction for " + first + " and " + second
                + " is: \t\t" + minus);
        System.out.println("Multiplication for " + first + " and " + second
                + " is: \t" + multiply);
        System.out.println("Division for " + first + " and " + second
                + " is: \t\t" + divide);
    }
}


Output:

Enter your first integer value: 
50
Enter your second integer value: 
50
Summation for 50 and 50 is:       100
Subtraction for 50 and 50 is:     0
Multiplication for 50 and 50 is:  2500
Division for 50 and 50 is:        1

3 comments: