Simple Calculator

Perform basic arithmetic operations.

Logic BuildingBeginner
Logic Building
# Take inputs
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))

# Calculate
if operator == '+':
    result = num1 + num2
elif operator == '-':
    result = num1 - num2
elif operator == '*':
    result = num1 * num2
elif operator == '/':
    if num2 != 0:
        result = num1 / num2
    else:
        result = "Error: Division by zero"
else:
    result = "Error: Invalid operator"

print(f"Result: {result}")

Output

Enter first number: 10
Enter operator (+, -, *, /): +
Enter second number: 5
Result: 15.0

Use conditionals for different operations.

Key Concepts:

  • Check operator type
  • Perform corresponding operation
  • Handle division by zero