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. By Pankaj Singh Last updated : December 20, 2023

Problem statement

Given two numbers and we have to design a calculator type application that will perform add, subtract, multiply and divide operations using Python.

Example

Consider the below example with sample input and output:

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

Python program to design a simple calculator

# 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

In this example, we have used the following Python topics that you should learn:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.