Answer to Question #274446 in Java | JSP | JSF for Arnold

Question #274446

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.


1
Expert's answer
2021-12-02T06:47:21-0500
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();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Arnold
03.12.21, 01:45

Thank you so much its a big help. I really recommend your site

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS