📘 Lesson  ·  Lesson 78

Calculator Program

Calculator Program

Calculator Program in Python

A simple calculator that performs the four basic operations based on the user's choice.

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

How it Works

  • The function checks the operator and returns the result.
  • Division guards against divide-by-zero.

Summary

  • A simple calculator that performs the four basic operations based on the user's choice.

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.

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

\n