Calculating the area of a circle is a common geometry problem that can be solved in many programming languages, including Python. In this tutorial, we will learn how to write a program that calculates the area of a circle in Python.
To start, we need to understand the formula for calculating the area of a circle.
Formula for Area of Circle
The formula is: Area = π * r^2
- where “Area” is the area of the circle.
- “π” (pi) is a mathematical constant approximately equal to 3.14, and
- “r” is the radius of the circle
Algorithm for Area of Circle
Here is a simple algorithm for calculating the area of a circle in Python:
- Ask the user to input the radius of the circle.
- Calculate the area of the circle using the formula:
area = pi * radius * radius
. - Print the area of the circle.
From the above algorithm, we understood how to implement a python program to calculate the area of the circle. Now it is time to perform it practically.
But before writing this program you have to know about:
- math module (Optional)
- how to take user input in python
- Python operators
Source Code
import math
radius = float(input('Enter radius of the Circle: '))
area = math.pi*(radius**2)
print(f"Area of Circle is {area}")
Output
Enter radius of the Circle: 2 Area of Circle is 12.566370614359172 |
Instead of writing math.pi
you can directly write the value of pi that is 3.14
Now let’s write the same program by using function.
Calculate Area of Circle Using Function
But before writing this program few programming concepts you have to know:
- math module (Optional)
- how to take user input in python
- Python operators
- Python Functions
Source Code
def circle_area(radius):
area = math.pi*(radius**2)
return area
radius = float(input('Enter radius of the Circle: '))
Area = circle_area(radius)
print(f"Area of Circle is {Area}")
Output
Enter radius of the Circle: 7 Area of Circle is 153.93804002589985 |