Python Program to Find the Area and Perimeter of the Circle

Problem Description

Write a Python Program to find the area and perimeter of the circle.

What is Area of 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.

What is the perimeter of a circle?

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

advertisement
advertisement

Here are the methods for finding the Area of a Circle and perimeter using Python.

Method 1: Area of Circle and perimeter using Math Module

In this approach, we calculate the area of a circle and perimeter using the math module.

Program/Source Code

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)
Program Explanation

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.

Runtime Test Cases

Testcase 1: Here, the given radius is “5”

advertisement
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

Method 2: Finds the area of the circle using classes

The program takes the radius from the user and finds the area of the circle using classes.

advertisement
Program/Source Code

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))
Program Explanation

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.

Runtime Test Cases

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”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.