Calculator
Source Code
package calculator;
import java.util.Scanner;
public class calculator
{
public static void main(String args[])
{
char operator;
double n1,n2,res;
System.out.println("Choose the operator: +,-,*,/");
Scanner s1=new Scanner(System.in);
operator=s1.next().charAt(0);
System.out.println("Enter the first number:");
n1=s1.nextInt();
System.out.println("Enter the second number:");
n2=s1.nextInt();
switch(operator)
{
case '+':
res=n1+n2;
System.out.println(n1+ "+"+ n2+"="+res);
break;
case '-':
res=n1-n2;
System.out.println(n1+ "-"+ n2+"="+res);
break;
case '*':
res=n1*n2;
System.out.println(n1+ "*"+ n2+"="+res);
break;
case '/':
res=n1/n2;
System.out.println(n1+ "/"+ n2+"="+res);
break;
}
}
}
0 Comments