Write a java program to create menu driven calculator that performs basic arithmetic operations using switch statement . Name the class as calculator. The program should ask the user to input 2 numbers and arithmetic operator (+,/,*,-). It should perform operation according to the entered and print the entered numbers , the operator and the result.
import java.util.Scanner;
public class Calculator {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter the number 1: ");
double number1 = keyBoard.nextDouble();
System.out.print("Enter the number 2: ");
double number2 = keyBoard.nextDouble();
keyBoard.nextLine();
System.out.print("Select arithmetic operator (+,/,*,-): ");
char arithmeticOperator = keyBoard.nextLine().charAt(0);
double result;
switch (arithmeticOperator) {
case '+':
result = number1 + number2;
System.out.printf("The sum of %.2f and %.2f is %.2f", number1, number2, result);
break;
case '/':
result = number1 / number2;
System.out.printf("The quotient of %.2f and %.2f is %.2f\n", number1, number2, result);
break;
case '*':
result = number1 * number2;
System.out.printf("The product of %.2f and %.2f is %.2f\n", number1, number2, result);
break;
case '-':
result = number1 - number2;
System.out.printf("The difference of %.2f and %.2f is %.2f\n", number1, number2, result);
break;
default:
System.out.printf("Wrong arithmetic operator");
break;
}
keyBoard.close();
}
}
Comments
Thank you so much its a big help. I really recommend your site
Leave a comment