Q:

Python | Design a simple calculator using if elif (just like switch case)

belongs to collection: Python basic programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Demonstrate an example of for loop... >>
<< Python | Example of Ternary Operator...