Home »
Python »
Python programs
Python | Design a simple calculator using if elif (just like switch case)
Python if else example: here, we are going to implement program to design a simple calculator using if, elif statements in Python that will perform add, subtract, multiply and divide operations.
Submitted by Pankaj Singh, on October 06, 2018
Given two numbers and we have to design a calculator type application that will perform add, subtract, multiply and divide operations using Python.
Example:
Message:
Calculator
1.Add
2.Substract
3.Multiply
4.Divide
Input:
Enter Choice(1-4): 3
Enter A:10
Enter B:20
Output:
Product = 200
Program:
# menus
print("Calculator")
print("1.Add")
print("2.Substract")
print("3.Multiply")
print("4.Divide")
# input choice
ch=int(input("Enter Choice(1-4): "))
if ch==1:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a+b
print("Sum = ",c)
elif ch==2:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a-b
print("Difference = ",c)
elif ch==3:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a*b
print("Product = ",c)
elif ch==4:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a/b
print("Quotient = ",c)
else:
print("Invalid Choice")
Output
Calculator
1.Add
2.Substract
3.Multiply
4.Divide
Enter Choice(1-4): 3
Enter A:10
Enter B:20
Product = 200
Python Basic Programs »
ADVERTISEMENT
ADVERTISEMENT