;

Python Program to Make a Simple Calculator


Tutorialsrack 22/04/2020 Python

In this Python program, we will learn how to make a simple calculator by which we can perform basic arithmetic operations such as addition, subtraction, multiplication, and division. In this program, we will use if...elif….else statement to perform these basic arithmetic operations.

Here is the code of the program to perform basic arithmetic operations.

Python Program to Make a Simple Calculator
# Python Program to Make a Simple Calculator

# Define a function to adds two numbers 
def add(x, y):
   return x + y

# Define a function to subtracts two numbers 
def subtract(x, y):
   return x - y

# Define a function to multiplies two numbers
def multiply(x, y):
   return x * y

# Define a function to divides two numbers
def divide(x, y):
   return x / y

print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

# Take input from the user 
option = input("Choose Option(1/2/3/4): ")

num1 = float(input("Enter the First Number: "))
num2 = float(input("Enter the Second Number: "))

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

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

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

elif option == '4':
   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("You choose the Invalid Option!!")
Output

Select operation.

1.Add

2.Subtract

3.Multiply

4.Divide

Choose Option(1/2/3/4): 1

Enter the First Number: 23

Enter the Second Number: 17

23.0 + 17.0 = 40.0


Related Posts



Comments

Recent Posts
Tags