# Calculator program in Java

Calculator program in Java is used by programmer to add, subtract, multiply or divide two integer numbers that are input by system. In the given example a class 'calculator' is used. System.in takes the input from the system/user at run time. InputStreamReader reads the input from System.in and then keep it into Buffer. Here is the Example of Calculator in Java program.

Tutorials

Calculator program in Java is used by programmer to add, subtract, multiply or divide two integer numbers that are input by system. In the given example a class "calculator" is used.

System.in takes the input from the system/user at run time. InputStreamReader reads the input from System.in and then keep it into Buffer.

parseInt method of Integer class is used to convert the strings into integers. If the user enters a "+" operator it will add the two integer numbers and display the result. If the user enters a "-" operator it will subtract the two numbers (if the first entered number is bigger then the second entered number the result will be a positive value otherwise it will be a negative value).

If the user enters a "*" operator it will multiply the two numbers. And if the users enters a "/" it will divide the two numbers (if the first entered number is bigger then the second entered number the result will be a integer value other wise it will be a fraction value)

Example of Calculator program in Java

```package Tutorial;

import java.io.*;

class calculator {
public static void main(String[] args) throws IOException {
System.in));
double x, y, store = 0;
char cal;
System.out.println("Enter the two numbers: ");
System.out.println("Enter the operator: ");

if (cal == '+') {
store = x + y;
} else if (cal == '-') {
if (x > y)
store = x - y;
if(x < y)
store= x - y;
} else if (cal == '*') {
store = x * y;
} else if (cal == '/') {
if (x > y)
store = x / y;
if( x < y)
store = x / y;

} else
System.out.println("Wrong operator: ");
System.out.println("The result is: " + store);
}

}```

Output:

Enter the two numbers:
12
10
Enter the operator:
*
The result is: 120

Enter the two numbers:
12
14
Enter the operator:
-
The result is: -2.0

Enter the two numbers:
12
6
Enter the operator:
/
The result is: 2.0
Enter the two numbers:
20
20
Enter the operator:
+

The result is: 40.0

# Calculator program in Java

Related Tutorials

Discuss: Calculator program in Java

Subject (*):

shalini
August 21, 2014
query related to notepad in java

how we can implement a mouseclicked event in notepad plzzz help me