Simple Calculator Program in Python-DecodingDevOps

Simple Calculator Program in Python

A calculator is a mechanical or electronic device with a keyboard and a small display that performs mathematical calculations. it is used to calculate in the sense of scheming. calculator synonyms include electronic calculator, pocket calculator, plotter, schemer, tables. Calculators display the output using the small display system.

# Program make a simple calculator
# This function adds two numbers 
def sum(x, y):
   return x + y

# This function subtracts two numbers 
def difference(x, y):
   return x - y

# This function multiplies two numbers
def product(x, y):
   return x * y

# This function divides two numbers
def divide(x, y):
   return x / y

print("Select operation.")

print("1.sum")

print("2.difference")

print("3.product")

print("4.Divide")

select = input("Enter choice(1/2/3/4): ")

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

if select == '1':
   print(num1,"+",num2,"=", sum(num1,num2))

elif select == '2':
   print(num1,"-",num2,"=", difference(num1,num2))

elif select == '3':
   print(num1,"*",num2,"=", product(num1,num2))

elif select == '4':
   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("Invalid input")

 

STEP 1: In this step, we define a function that is used to add both the numbers.

STEP 2: In this step, we define a function that is used to subtract both the numbers.

STEP 3: In this step, we define a function that is used to multiply both the numbers.

STEP 4: In this step, we define a function that is used to divide both the numbers.

STEP 5: In this step, give a particular choice to select any one of the following operations which include addition, subtraction, multiplication, division.

STEP 6: In this step, we take the input from the user and define two numbers on which the operations should be performed.

STEP 7: In this step, using the if and nested if loop we perform various operations and based on the choice given we will print the operation of that particular choice on the two numbers and print the result.

OUTPUT:

Select operation.
1.sum
2.difference
3.product
4.Divide
Enter select(1/2/3/4): 3
Enter first number: 12
Enter second number: 10
12.0 * 10.0 = 120.0

 

Leave a Reply

Your email address will not be published. Required fields are marked *