📘 Lesson  ·  Lesson 78

Calculator Program

Calculator Program in Python

एक simple calculator जो user की पसंद के आधार पर चार basic operations करता है।

Python Program

Python
def calculator(a, b, op):
    if op == "+":
        return a + b
    elif op == "-":
        return a - b
    elif op == "*":
        return a * b
    elif op == "/":
        return a / b if b != 0 else "Cannot divide by zero"
    return "Invalid operator"

print(calculator(10, 5, "+"))  # 15
print(calculator(10, 5, "*"))  # 50
print(calculator(10, 0, "/"))

Expected Output

15 50 Cannot divide by zero

कैसे काम करता है

  • Function operator जाँचकर result लौटाता है।
  • Division divide-by-zero से बचाता है।

सारांश

  • एक simple calculator जो user की पसंद के आधार पर चार basic operations करता है।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.