Write a java program for arithmetic operation by taking input as arithmetic operator like '+', '-' , '*' , '/' , '%' using switch case
Answers
Answered by
1
Question:-
Write a java program for arithmetic operation by taking input as arithmetic operator like '+', '-' , '*' , '/' , '%' using switch case.
Program:-
import java.util.*;
class Calculation
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt();
System.out.print("Enter the second number: ");
int b=sc.nextInt();
System.out.println("Enter your choice, +, -, *, /, %");
char ch=sc.next().charAt(0);
int result=0;
boolean x=true;
switch(ch)
{
case '+': result=a+b;
break;
case '-': result=a-b;
break;
case '*': result=a*b;
break;
case '/': result=a/b;
break;
case '%': result=a%b
break;
default: System.out.println("Invalid Choice.");
x=false;
}
if(x)
System.out.println("Result is: "+result);
}
}
Similar questions