Computer Science, asked by shrikanty722, 9 months ago

Write a python program to make an Calculator programming ?

Answers

Answered by tiger009
2

Python program of calculator

Output:

Select operations.

1.Addition

2.Subtraction

3.Multiplication

4.Division

Enter choice(1/2/3/4): 4

Enter first number: 15

Enter second number: 3

15.0 / 3.0 = 5.0

Explanation:

Following are the program in the Python Programming Language

#define function for addition

def add(a, b):

 #return the value of addition

 return a + b

#define function for subtraction

def subtract(a, b):

 #return the value of subtraction

 return a - b

#define function of multiplication

def multiply(a, b):

 #retun the value of multiplication

 return a * b

#define the function of divide

def divide(a, b):

 #return the value of divide

 return a / b

#print message for the selection

print("Select operations.")

#for addition

print("1.Addition")

#for subtraction

print("2.Subtraction")

#for multiplication

print("3.Multiplication")

#for divide

print("4.Division")

#get input from the user for selection

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

#get 1st value from the user

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

#get 2nd value from the user

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

#check condition if the selection is one

if oprt == '1':

 #print the result of addition

 print(inp1,"+",inp2,"=", add(inp1,inp2))

#check condition if the selection is 2

elif oprt == '2':

 #print the result of subtraction

 print(inp1,"-",inp2,"=", subtract(inp1,inp2))

#check condition if the selection is 3

elif oprt == '3':

 #print the result of multiplication

 print(inp1,"*",inp2,"=", multiply(inp1,inp2))

#check condition if the selection is 4

elif oprt == '4':

 #print the result of division

 print(inp1,"/",inp2,"=", divide(inp1,inp2))

#check condition if the selection is wrong

else:

 #print message invalid

 print("Invalid input")

Following are the description of the program:

  • Firstly, we define different functions for the addition, subtraction, multiplication and division.
  • Then, print the messages for the user to select their desired selections.
  • Then, we set the if conditional statements according to the selection of the user. If they select 1 then, the program ask for the input and it call and print the addition function. This is same for till the selection of 4, otherwise it print 'Invalid input'.

Learn More:

brainly.in/question/12040986

Answered by Equestriadash
13

choice = "Yes"

while choice == "Yes":

   print("Simple calculator!", "******************", sep = "\n")

   print("1. Addition", "2. Subtraction", "3. Multiplication", "4. Division", sep = "\n")

   x = int(input("Enter a value: "))

   y = int(input("Enter a value: "))

   z = int(input("Enter the operation you'd like us to perform: "))

   if z == 1:

       print(x + y)

   elif z == 2:

       print(x - y)

   elif z == 3:

       print(x*y)

   elif z == 3:

       if y == 0:

           print("Division isn't possible.")

       else:

           print(x/y)

   print("Do you wish to perform more operations?")

   print("Yes/No")

   choice = input()

Similar questions