Python – Program to Read the symbol of a binary ARITHMETIC Operators (+, -, *, /, **, //, %) and do the Mathematical calculations

<code data-enlighter-language="python" class="EnlighterJSRAW">operand1 = float(input("Enter the first value: "))
operator = input("Enter a binary arithmetic operator (+, -, *, /, **, //, %): ")
operand2 = float(input("Enter the second second: "))

if operator == "-":
    result = operand1 - operand2
elif operator == "*":
    result = operand1 * operand2
elif operator == "+":
    result = operand1 + operand2
elif operator == "/":
    if operand2 != 0:
        result = operand1 / operand2
    else:
        result = "Error: Division by zero"
elif operator == "**":
    result = operand1 ** operand2
elif operator == "//":
    if operand2 != 0:
        result = operand1 // operand2
    else:
        result = "Error: Floor division by zero"
elif operator == "%":
    if operand2 != 0:
        result = operand1 % operand2
    else:
        result = "Error: Modulo division by zero"
else:
    result = "Error: Invalid operator"

print(f"{operand1} {operator} {operand2} = {result}")
Scroll to Top
logo
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.