cs/practical-file/outputs/question_07_output.txt
2025-06-24 02:37:47 +05:30

35 lines
859 B
Plaintext

Question 07:
Write a Python program to perform arithmetic calculation. The program should accept two operands and an operator, then display the result.
Answer:
def main(num1: float, num2: float, operator: str) -> None:
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
if num2 == 0:
print("Error: Division by zero")
raise ZeroDivisionError
result = num1 / num2
else:
print("Error: Invalid operator")
raise ValueError("Invalid operator. Use +, -, *, or /.")
print(f"Result: {result}")
if __name__ == '__main__':
main(10, 5, '+')
main(10, 5, '-')
main(10, 5, '*')
main(10, 5, '/')
Output:
Result: 15
Result: 5
Result: 50
Result: 2.0