📘 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 करता है।
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.