WAP to perform 4 basic mathematical calculations (addition,subtraction,multiplication and division with remainder and quotient part) with two double data type variables in switch case
print your result other than 1 to 4 print a message for a wrong entry
Answers
Answer:
Calculator using switch-case in Java.
Explanation:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter two numbers: ");
// nextDouble() reads the next double from the keyboard
double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = reader.next().charAt(0);
double result;
switch(operator)
{
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
// operator doesn't match any case constant (+, -, *, /)
default:
System.out.printf("Error! 0perator is not correct (wrong entry)");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
}
}
SAMPLE OUTPUT:
Enter two numbers: 1.5
4.5
Enter an operator (+, -, *, /): *
1.5 * 4.5 = 6.8