Python Program to Calculate Perimeter of Circle
perimeter = 2 * 3.1416 * radius, where radius is the circle's radius.Examples
How to Think About It
2 * π * radius. So, you get the radius from the user, multiply it by 2 and π (about 3.1416), and that gives you the perimeter.Algorithm
Code
radius = float(input("Enter the radius of the circle: ")) pi = 3.1416 perimeter = 2 * pi * radius print("Perimeter of circle:", perimeter)
Dry Run
Let's trace the example where radius = 5 through the code
Input radius
User enters 5, so radius = 5.0
Set pi
pi = 3.1416
Calculate perimeter
perimeter = 2 * 3.1416 * 5.0 = 31.416
Print result
Output: Perimeter of circle: 31.416
| Step | Variable | Value |
|---|---|---|
| 1 | radius | 5.0 |
| 2 | pi | 3.1416 |
| 3 | perimeter | 31.416 |
| 4 | output | Perimeter of circle: 31.416 |
Why This Works
Step 1: Get radius input
We use input() to get the radius from the user and convert it to a number with float() so we can do math.
Step 2: Use pi value
We set pi to 3.1416, which is a close value for π, the circle constant.
Step 3: Calculate perimeter
We multiply 2 * pi * radius to find the perimeter, which is the distance around the circle.
Step 4: Show result
We print the perimeter so the user can see the answer.
Alternative Approaches
import math radius = float(input("Enter the radius: ")) perimeter = 2 * math.pi * radius print("Perimeter of circle:", perimeter)
def circle_perimeter(r): return 2 * 3.1416 * r radius = float(input("Radius: ")) print("Perimeter:", circle_perimeter(radius))
Complexity: O(1) time, O(1) space
Time Complexity
The program does a fixed number of operations regardless of input size, so it runs in constant time.
Space Complexity
Only a few variables are used, so memory use is constant.
Which Approach is Fastest?
All approaches run instantly for any input size; using math.pi is preferred for accuracy without speed loss.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Fixed pi value | O(1) | O(1) | Simple quick calculation |
| math.pi constant | O(1) | O(1) | Accurate calculations |
| Function encapsulation | O(1) | O(1) | Reusable code |
math.pi for better precision instead of a fixed number.