Write a Python Program to find the area and perimeter of the circle.
Area of circle is defined as pi*r*r where pi is a constant whose value is (22/7 or 3.142) and r is the radius of a circle.
Formula to calculate the area of circle is: Area = pi*r*r
Example:
If the radius of a circle is 5, then area of circle is pi*r*r = 3.14*5*5 = 78.5.
The perimeter of a circle is also known as the circumference. It refers to the total length of the boundary or the distance around the circle.
Formula to calculate the area of Perimeter = 2 * π * radius
Example:
If the radius of a circle is 5, then area of circle is 2 * π * radius = 2*3.14*5 = 31.4
Here are the methods for finding the Area of a Circle and perimeter using Python.
In this approach, we calculate the area of a circle and perimeter using the math module.
Here is the source code of the Python Program to take the radius from the user and find the area of the circle using math module. The program output is also shown below.
import math radius = float(input("Enter the radius of the circle: ")) # Calculate the area of the circle area = math.pi * radius ** 2 # Calculate the perimeter of the circle perimeter = 2 * math.pi * radius print("Area of the circle:", area) print("Perimeter of the circle:", perimeter)
1. In this program, we first prompt the user to enter the radius of the circle.
2. Then, using the formula area = pi * radius2, we calculate the area of the circle.
3. Next, using the formula perimeter = 2 * pi * radius, we calculate the perimeter of the circle.
4. Finally, we display the calculated area and perimeter.
Time Complexity: O(1)
The time complexity of the program is O(1) as the calculations are performed in constant time regardless of the input size.
Space Complexity: O(1)
The space complexity is also O(1) as the program uses a fixed amount of memory to store the variables and the math module.
Testcase 1: Here, the given radius is “5”
Enter the radius of the circle: 5 Area of circle: 78.54 Perimeter of circle: 31.42
Testcase 2: Here, the given radius is “10”
Enter the radius of the circle: 10 Area of the circle: 314.16 Perimeter of the circle: 62.83
The program takes the radius from the user and finds the area of the circle using classes.
Here is the source code of the Python Program to take the radius from the user and find the area of the circle using classes. The program output is also shown below.
import math class circle(): def __init__(self,radius): self.radius=radius def area(self): return math.pi*(self.radius**2) def perimeter(self): return 2*math.pi*self.radius r=int(input("Enter radius of circle: ")) obj=circle(r) print("Area of circle:",round(obj.area(),2)) print("Perimeter of circle:",round(obj.perimeter(),2))
1. User must enter the value of radius.
2. A class called circle is created and the __init__() method is used to initialize values of that class.
3. A method called as area returns math.pi*(self.radius**2) which is the area of the class.
3. Another method called perimeter returns 2*math.pi*self.radius which is the perimeter of the class.
5. An object for the class is created.
6. Using the object, the methods area() and perimeter() are called.
7. The area and perimeter of the circle is printed.
Time Complexity: O(1)
The time complexity of the program is O(1) as the calculations are performed in constant time regardless of the input size.
Space Complexity: O(1)
The space complexity is also O(1) as the program uses a fixed amount of memory to store the variables and the math module, and the space required does not depend on the input size.
Testcase 1: Here, the given radius is “5”
Enter radius of circle: 5 Area of circle: 78.54 Perimeter of circle: 31.42
Testcase 2: Here, the given radius is “10”
Enter radius of circle: 10 Area of circle: 314.16 Perimeter of circle: 62.83
To practice programs on every topic in Python, please visit “Programming Examples in Python”.
- Apply for Python Internship
- Apply for Programming Internship
- Check Information Technology Books
- Practice Programming MCQs
- Check Python Books