Python Program to Find Area of Circle
To find the area of a circle in Python, use the formula
area = 3.14159 * radius ** 2 where radius is the circle's radius; for example, area = 3.14159 * r ** 2.Examples
Inputradius = 0
OutputArea of the circle is 0.0
Inputradius = 5
OutputArea of the circle is 78.53975
Inputradius = 10.5
OutputArea of the circle is 346.3600725
How to Think About It
To find the area of a circle, first understand that the area depends on the radius squared multiplied by pi (about 3.14159). You take the radius number, multiply it by itself, then multiply by pi to get the area.
Algorithm
1
Get the radius value from the user2
Calculate the area by multiplying pi with radius squared3
Display the calculated areaCode
python
radius = float(input("Enter the radius of the circle: ")) pi = 3.14159 area = pi * radius ** 2 print(f"Area of the circle is {area}")
Output
Enter the radius of the circle: 5
Area of the circle is 78.53975
Dry Run
Let's trace the program with radius = 5 through the code
1
Input radius
User enters 5, so radius = 5.0
2
Calculate area
area = 3.14159 * 5.0 ** 2 = 3.14159 * 25 = 78.53975
3
Print result
Output: Area of the circle is 78.53975
| Step | Variable | Value |
|---|---|---|
| 1 | radius | 5.0 |
| 2 | area | 78.53975 |
Why This Works
Step 1: Input radius
We get the radius from the user as a floating-point number using float(input()) to allow decimal values.
Step 2: Calculate area
We use the formula area = pi * radius ** 2 where ** means 'to the power of'. This calculates the area.
Step 3: Display area
We print the result using an f-string to show the area clearly.
Alternative Approaches
Using math.pi constant
python
import math radius = float(input("Enter the radius of the circle: ")) area = math.pi * radius ** 2 print(f"Area of the circle is {area}")
This uses Python's built-in math.pi for more precise pi value.
Using a function
python
def circle_area(r): pi = 3.14159 return pi * r ** 2 radius = float(input("Enter the radius: ")) print(f"Area of the circle is {circle_area(radius)}")
Encapsulates calculation in a function for reuse.
Complexity: O(1) time, O(1) space
Time Complexity
The calculation involves only a few arithmetic operations, so it runs in constant time.
Space Complexity
Only a few variables are used, so the space needed is constant.
Which Approach is Fastest?
All approaches run in constant time; using math.pi is slightly more precise but similar in speed.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Fixed pi value | O(1) | O(1) | Simple quick calculation |
| math.pi constant | O(1) | O(1) | More precise calculation |
| Function encapsulation | O(1) | O(1) | Reusable code structure |
Use
math.pi for a more accurate value of pi instead of a fixed number.Forgetting to square the radius and using
radius * 2 instead of radius ** 2.