Write a Java program to print the result of the following operations. Test Data:
a. -5+8*6
b. (55+9) % 9
c. 20+-3 5/8
d. 5+15/3 2-8% 3
Answers
Answer:
a is correct please like and follow
Switch should be used such that the operators are taken from the user and numbers also.
Explanation:
public class abc
{
public static void main(String[] args)
{
double firstNum;
double secondNum;
char operator;
double value;
System.out.println("Enter expressions such as 2 + 2 or 34.2 * 7.81");
System.out.println("using any of the operators +, -, *, /.");
System.out.println("To end, enter a 0.");
System.out.println();
while (true) {
System.out.print("? ");
firstNum = TextIO.getDouble();
if (firstNum == 0)
break;
operator = TextIO.getChar();
secondNum = TextIO.getlnDouble();
switch (operator)
{
case '+':
value = firstNum + secondNum;
break;
case '-':
value = firstNum - secondNum;
break;
case '*':
value = firstNum * secondNum;
break;
case '/':
value = firstNum / secondNum;
break;
default:
System.out.println("Unknown operator: " + operator);
continue;
} // end switch
System.out.println("Value is " + value);
System.out.println();
}
System.out.println("done");
}
}