Java Switch case program arithmetic operation /simple calculator
import java.util.Scanner;
public class Calculator
{
public static void main(String args[ ])
{
Scanner s=new Scanner(System.in);
double a, b;
char operator;
System.out.println("Enter the value of a and b");
a=s.nextDouble();
b=s.nextDouble();
System.out.println("Enter any operator :+ , - , * , / ");
operator=s.next().charAt(0);
double addition =a+b;
double subtraction =a-b;
double multiplication =a*b;
double division =a/b;
switch(operator)
{
case '+': System. out.println("Addition of a and b :"+addition);
break;
case '-': System.out.println("Subtraction of a and b :"+subtraction );
break;
case '*' : System.out.println("Multiplication of a and b :"+multiplication);
break;
case '/ ' : System.out.println("Divide b from a :"+division);
break;
default :System.out.println("Enter valid operator ");
}
}
}
Output
Enter the value of a and b
2
1
Enter any operator:+ , - , * , /
-
Subtraction of a and b :1.0
Comments