brel-logo-white brel-logo-white
  • Travel
  • Case Study
  • How to
  • Technology
  • More
    • Sports
    • Health
    • Articles
    • Review
Notification
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
BreldigitalBreldigital
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
Search
  • Quick Access
    • Home
    • History
    • My Saves
    • My Interests
    • My Feed
  • Categories
    • Travel
    • Sports
    • Health

Top Stories

Explore the latest updated news!

Stephanie puts 30 cubes in a box

Which statement correctly describes the process that occurs in the thylakoid?

Which point is on the line that passes through point h and is perpendicular to line fg?

Stay Connected

Find us on socials
248.1k Followers Like
61.1k Followers Follow
165k Subscribers Subscribe
Breldigital > Blog > ds > Create a calculator in python
ds

Create a calculator in python

By John Published November 10, 2022
Share

This is a Python Program to find the area of a rectangle using classes.

Problem Description
The program takes the length and breadth from the user and finds the area of the rectangle using classes.

Problem Solution
1. Create a class and using a constructor to initialize values of that class.
2. Create methods for adding, substracting, multiplying and dividing two numbers and returning the respective results.
3. Take the two numbers as inputs and create an object for the class passing the two numbers as parameters to the class.
4. Using the object, call the respective function depending on the choice taken from the user.
5. Print the final result.
6. Exit

Program/Source Code
Here is the source code of the Python Program to take the length and breadth from the user and find the area of the rectangle. The program output is also shown below.

class cal():
def __init__(self,a,b):
self.a=a
self.b=b
def add(self):
return self.a+self.b
def mul(self):
return self.a*self.b
def div(self):
return self.a/self.b
def sub(self):
return self.a-self.b
a=int(input(“Enter first number: “))
b=int(input(“Enter second number: “))
obj=cal(a,b)
choice=1
while choice!=0:
print(“0. Exit”)
print(“1. Add”)
print(“2. Subtraction”)
print(“3. Multiplication”)
print(“4. Division”)
choice=int(input(“Enter choice: “))
if choice==1:
print(“Result: “,obj.add())
elif choice==2:
print(“Result: “,obj.sub())
elif choice==3:
print(“Result: “,obj.mul())
elif choice==4:
print(“Result: “,round(obj.div(),2))
elif choice==0:
print(“Exiting!”)
else:
print(“Invalid choice!!”)

print()

Runtime Test Cases

Case 1:
Enter first number: 2
Enter second number: 4
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
Enter choice: 1
Result: 6
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
Enter choice: 3
Result: 8
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
Enter choice: 0
Exiting!

Case 2:
Enter first number: 150
Enter second number: 50
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
Enter choice: 2
Result: 100
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
Enter choice: 4
Result: 3.0
0. Exit
1. Add
2. Subtraction
3. Multiplication
4. Division
Enter choice: 0
Exiting!

TAGGED: Create a calculator in python
John November 10, 2022 November 10, 2022

Search

brel-logo-white brel-logo-white
Explore a wide range of brands and categories with our comprehensive coverage, and stay up-to-date with the latest news and trends by subscribing to our updates.
Categories:
  • Entertainment
  • Travel
  • Sport
  • Contact Us

Quick Links

  • My Feed
  • My Interests
  • History
  • My Saves

About US

  • Adverts
  • Our Jobs
  • Term of Use

© 2023 Brel Digital All Rights Reserved. All logos and images used on this website are registered trademarks of their respective companies

Welcome Back!

Sign in to your account

Lost your password?