Site icon Matistics

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}")
Exit mobile version