# Java program for Calculator - Design calculator with arithmetic operators

Java Program for Calculator - This program will read two integer numbers and calculate the arithmetic operators, in this example we used switch case and if else statement. User will enter a choice after entering two numbers and based on user choice program will return the result.

## Calculator Example using Java Program

```/*Java program for Calculator.*/

import java.util.*;

public class Calculator{

public static void main(String []args){
int a,b,choice;
float result=0;
/*scanner class object to read values*/
Scanner buf=new Scanner(System.in);

System.out.print("Enter first number: ");
a=buf.nextInt();
System.out.print("Enter second number: ");
b=buf.nextInt();

System.out.print("\n3: Multiplication.\n4: Divide.");
System.out.print("\n5: Remainder.\n6: Exit.");

choice=buf.nextInt();

switch(choice)
{
case 1:
result=(a+b); break;
case 2:
result=(a-b); break;
case 3:
result=(a*b); break;
case 4:
result=(float)((float)a/(float)b); break;
case 5:
result=(a%b); break;
default:
System.out.println("An Invalid Choice!!!\n");
}
if(choice>=1 && choice<=5)
System.out.println("Result is: " + result);

}
}
```

Output

```
First Run:
[email protected]:~\$ javac Calculator.java

[email protected]:~\$ java Calculator
Enter first number: 10
Enter second number: 20

2: Subtraction.
3: Multiplication.
4: Divide.
5: Remainder.
6: Exit.
Result is: 0.5

Second Run:
[email protected]:~\$ java Calculator
Enter first number: 10
Enter second number: 7

2: Subtraction.
3: Multiplication.
4: Divide.
5: Remainder.
6: Exit.
Result is: 3.0

```