Write a java program to input 2 numbers and an operator. Using switch case perform the calculation as per the operator ie + or - or * or / or %
Answers
Answer:
Explanation:
import java.util.*;
class calculation
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int sum,sub,div,mul,x,a,b;
System.out.println("Enter your Choice");
System.out.println("1.Addition");
System.out.println("2.subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
x=sc.nextInt();
switch(x)
{
case 1:
System.out.println("Enter first Number");
a=sc.nextInt();
System.out.println("Enter second number");
b=sc.nextInt();
sum=a+b;
System.out.println("The Sum is : " + sum);
break;
case 2:
System.out.println("Enter first Number");
a=sc.nextInt();
System.out.println("Enter second number");
b=sc.nextInt();
sub=a - b;
System.out.println("The subtraction is : " + sub);
break;
case 3:
System.out.println("Enter first Number");
a=sc.nextInt();
System.out.println("Enter second number");
b=sc.nextInt();
mul=a+b;
System.out.println("The Multiplication is : " + mul);
break;
case 4:
System.out.println("Enter first Number");
a=sc.nextInt();
System.out.println("Enter second number");
b=sc.nextInt();
div=a+b;
System.out.println("The Division is : " + div);
break;
default:
System.out.println("Wrong choice");
break;
}
}
}