Skip to content

Commit 28593a7

Browse files
Area and Perimeter of the Circle - Classes Practice Problems
1 parent a4dc25e commit 28593a7

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Area and Perimeter of the Circle - Classes Practice Problems
3+
Design a class with has 2 methods. One which calculates area of the circle and one which calculates the
4+
circumference of a circle given the radius r. Please use the pi value as 3.14. For any infeasible radius
5+
r, please return the area and circumference as 0.0
6+
7+
Your class should be named Circle. Method to get area should be named getArea. Method to get
8+
circumference should be named getCircumference.
9+
10+
Input
11+
One line containing a floating point number denoting the radius r.
12+
13+
Output
14+
2 lines containing floating point numbers. First one containing the area of the circle. second line
15+
containing the circumference of the circle.
16+
17+
Example
18+
Input:
19+
20+
5
21+
Output:
22+
23+
78.5
24+
31.4
25+
First line in input is radius r which is 5. Area is 5*5*3.14 which is 78.5 which is the first line of the
26+
output. Circumference is 2*3.14*5 which is 31.4 which is the second line of the output.
27+
28+
'''
29+
30+
# Your class should be named `Circle`.
31+
# Method to get area should be named `getArea`
32+
# Method to get circumference should be named `getCircumference`.
33+
# Your class should take radius `r` as input when creating an object.
34+
class Circle:
35+
def __init__(self,r):
36+
self.r=r
37+
def getArea(self):
38+
if self.r>0:
39+
return 3.14*((self.r)**2)
40+
else:
41+
return 0
42+
def getCircumference(self):
43+
if self.r>0:
44+
return 2*(3.14*self.r)
45+
else:
46+
return 0
47+
if __name__ == "__main__":
48+
one_circle = Circle(float(input()))
49+
print(float(one_circle.getArea()))
50+
print(float(one_circle.getCircumference()))

0 commit comments

Comments
 (0)