42. Python Program to Perform Arithmetic Calculations using Switch Case.
How does this program work?
- In this program we are going to learn about how to perform arithmetic calculations by using Switch Case in Python.
- The integer entered by the user is stored in two variables a, b and also entered choice is stored in variable option.
- By using switch case select one option then equivalent operation (Addtion, Subtraction, Multiplication, Division) can be performed by given logic.
Here is the code
def switch():
a = int(input("Enter first value: "))
b = int(input("Enter second value: "))
print("Press 1 for Addittion \nPress 2 for Subtraction \nPress 3 for Multiplication \nPress 4 for Division")
option = int(input("Enter your option: "))
if option == 1:
result = a+b
print("Addition : ", result)
elif option == 2:
result = a-b
print("Subtraction : ",result)
elif option == 3:
result = a*b
print("Multiplication : ", result)
elif option == 4:
result = a/b
print("Division : ",result)
else:
print("Invalid Value")
switch()