top of page
Search

# Program to make Calculator in Python

Updated: Jul 29, 2022

Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user input. Approach :

1. Choose the desired operation from the option of a, b, c, and d.

2. Choose two numbers, and if… elif… else, branching is used for executing the particular operation.

3. Use add(), subtract(), multiply() and divide() function for evaluation the respective operation in the calculator. ### Code:

```# add
return x + y

# subtract
def subtract(x, y):
return x - y

#  multiply
def multiply(x, y):
return x * y

# divide
def divide(x, y):
return x / y

print("Select the operation.")
print("2.Subtraction")
print("3.Multiplication")
print("4.Division")

while True:
choice = input("Enter choice(1, 2, 3, 4): ")

if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))

elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))

elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))

elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))

else:
print("Invalid Input")```

### Output: The Tech Platform