Ask Question
20 March, 12:02

Write a program that prompts the user to enter an equation in the form of 10 5, or 10-5, or 1*5, or 13/4, or 13%4. The program should then output the equation, followed by an equal sign, and followed by the answer.

+5
Answers (1)
  1. 20 March, 14:02
    0
    equation = input ("Enter an equation: ") if ("+" in equation) : operands = equation. split ("+") result = int (operands [0]) + int (operands[1]) print (operands[0] + "+" + operands[1] + "=" + str (result)) elif ("-" in equation) : operands = equation. split ("-") result = int (operands [0]) - int (operands[1]) print (operands[0] + "-" + operands[1] + "=" + str (result)) elif ("*" in equation) : operands = equation. split ("*") result = int (operands [0]) * int (operands[1]) print (operands[0] + "*" + operands[1] + "=" + str (result)) elif ("/" in equation) : operands = equation. split ("/") result = int (operands [0]) / int (operands[1]) print (operands[0] + "/" + operands[1] + "=" + str (result)) elif ("%" in equation) : operands = equation. split ("%") result = int (operands [0]) % int (operands[1]) print (operands[0] + "%" + operands[1] + "=" + str (result))

    Explanation:

    The solution code is written in Python 3.

    Firstly prompt user to enter an equation using input function (Line 1).

    Create if-else if statements to check if operator "+", "-", "*", "/" and "%" exist in the input equation. If "+" is found (Line 3), use split method to get the individual operands from the equation by using "+" as separator (Line 5). Output the equation as required by the question using string concatenation method (Line 6). The similar process is repeated for the rest of else if blocks (Line 7 - 22).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that prompts the user to enter an equation in the form of 10 5, or 10-5, or 1*5, or 13/4, or 13%4. The program should then ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers