Calculator Using Switch Case in Java
import java.util.*;
class CalculatorUsingSwitchCase
{
public static void main(String[] args)
{
char operator;
double result;
Scanner input = new Scanner(System.in);
System.out.println("Enter First Number");
double n1 = input.nextDouble();
System.out.println("Enter Second Number");
double n2 = input.nextDouble();
System.out.println("Choose an Opeartor : + , - , * , / , or %");
operator = input.next().charAt(0);
switch (operator)
{
case '+' :
result = n1 + n2;
System.out.println("Addition of " +n1+ " and " + n2+ " is " + result);
break;
case '-' :
result = n1-n2;
System.out.println("Subraction of " +n1+ " and " + n2+ " is " + result);
break;
case '*' :
result = n1*n2;
System.out.println("Multiplication of " +n1+ " and " + n2+ " is " + result);
break;
case '/' :
result = n1/n2;
System.out.println("Division of " +n1+ " and " + n2+ " is " + result);
break;
case '%' :
result =(n1*n2)/100;
System.out.println("Percntage of " +n1+ " and " + n2+ " is " + result);
break;
default :
System.out.println("Invalid Case");
break;
}
input.close();
}
}
Input :-
Enter First Number :-20
Enter Second Number :-14
Choose an Operator : +
Output :-
Addition of 20 and 14 is 34
👍👍
ReplyDelete