Ask Question
29 July, 00:13

Write a simple calculator program. Your program should ask for three things two whole numbers and an operator in the form of an expression like: 3 * 2 Use a select case structure to determine what operation needs to be performed on the two numbers. Your program should handle the arithmetic functions Add, Subtract, Multiply, and Divide (Depending on the operator entered).

+5
Answers (1)
  1. 29 July, 00:38
    0
    The solution code is written in Java.

    Scanner input = new Scanner (System. in); System. out. print ("Enter operator: "); String operator = input. nextLine (); System. out. print ("Enter first integer: "); int num1 = input. nextInt (); System. out. print ("Enter second integer: "); int num2 = input. nextInt (); int result = 0; switch (operator) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; default: System. out. println ("Invalid operator"); } System. out. println (result);

    Explanation:

    To ask for the user input for two whole numbers and an operator, we can use Java Scanner class object. Since the input operator is a string, we can use nextLine () method to get the operator string (Line 3). We use nextInt () method to get whole number input (Line 5 & 7).

    Next we use the switch keyword and pass the operator into the switch structure to determine which case statement should be executed. For example, if the input operator is "*" the statement "result = num1 * num2; " will run and multiply num1 with num2.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a simple calculator program. Your program should ask for three things two whole numbers and an operator in the form of an expression ...” in 📗 Engineering if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers