Python Program to Find Area of a Circle
In this tutorial, we will learn how to find the area of a circle using the Python programing language. Previously, we saw how to find the area of a square, we will follow a similar approach and use the formula for calculating the area of a circle.
Formula:
Our program should take input as follows and display area as output.
For a circle with a radius of 7 cm, the area of a circle is 154cm
Input- Radius: 7
Output- Area: 154
Look at the algorithm below to understand the working.
Algorithm
Step 1- Define a function area_of_circle()
Step 2- Set pi as constant and initialize to 3.147
Step 3- Declare variable area to calculate and store area
Step 4- Return area
Step 5- Take input of radius from the user
Step 6- Pass radius in the function
Step 7- Print the result
Python Program
Look at the program to understand the implementation of the above-mentioned approach.
def area_of_circle(r):
pi=3.147
area= pi*r*r
return area
radius=float(input("Enter Radius: "))
print("Area: ",area_of_circle(radius))
Enter Radius: 3.2
Area: 32.22528
To calculate area, we can also use the pow() function which is a function in the math class of the Python library.
The pow(x,y) returns the value of x raised to the power y.
In place of pi*r*r in #line 8 of the code, we can also write pi*pow(r,2) to calculate area.
To know about Python math library functions in detail check out this article.
Conclusion
So far, we have learned how to define a function to calculate the area of a circle where the radius is given by the user in Python. In the function body, we have set pi as a constant value and put it in the formula along with the radius to calculate the area. Finally, the area was returned by the function and displayed using the print() function.