Python program to find perimeter of circle

In this tutorial, we will learn how to find the perimeter/circumference of a circle in Python. The logic for this program is quite easy if you know the formula to calculate the perimeter/circumference of a circle.

It’s always better to understand the concepts through coding. We’ll try to scribble a simple code to find the circumference of a circle. This code will take a float radius as input from the user, and it will give circumference as output.

from math import pi

radius=float(input("Enter radius: "))

Here we have imported pi from the math module as we will need it to calculate the perimeter. We have read input from user in the float form considering the possibility that the radius can be of float type.

circum=2*pi*radius

print(f"Circumference is {round(circum,2)}")

we have calculated the perimeter of a circle using 2*pi*r. To restrict the floating-point precision to 2 places we have used a pre-defined function round().

Output:
Enter radius: 5
Circumference is 31.42

Example

Let’s consider another example, here we will find the circumference of the circle by using a user-defined function.

from math import pi

def circum(r):
    return 2*pi*r

radius=float(input("Enter radius: "))
print(f"Circumference is {round(circum(radius),2)}")

here we have performed the same task as earlier, but here we have constructed a user-defined function to get the job done. There is a more professional approach to solve the problem.

Output:
Enter radius: 5
Circumference is 31.42

 

This is how we can easily find the circumference of a circle.

Leave a Reply

Your email address will not be published. Required fields are marked *