Question: Write a program that reads in an operator and two operands, performs the operation, and then prints out the result. Input The input will be
Write a program that reads in an operator and two operands, performs the operation, and then prints out the result.
Input
The input will be one or more lines of numbers that have been typed in by the user. Each line is to have an operator followed by two operands. The operator can be one of the seven: +, -, *, /, %, power, and average.
Output
The output will have for each line, the first operand followed by a space followed by the operator followed by a space followed by the second operand followed by a space followed by = followed by a space followed by the result.
Each number is to be printed using "%.2e" format.
Sample Input
+ 2 3
power 2.2 3
average 2.2 1.3
Sample Output
2.00e+00 + 3.00e+00 = 5.00e+00
2.20e+00 power 3.00e+00 = 1.06e+01
2.20e+00 average 1.30e+00 = 1.75e+00
USE THE HINT 1. Here use in.next() to read the operator and in.nextDouble() to read a number. 2. After reading the two doubles, use in.nextLine() so that you discard the rest of the "white spaces". 3. To print two numbers num1 and num2 using %.2e format and printf 4. Use String class's equals method to compare two strings: example if (s.equals("+")) { }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
