Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Circle Properties Calculation

Description:
This algorithm calculates the area and circumference of a circle given its radius.
It uses the formulas:
Area = π * r^2
Circumference = 2 * π * r

Approach:
- Take the radius as input
- Compute area and circumference
- Return both values

Use Cases:
- Geometry calculations
- Basic math learning
- DSA practice examples

Time Complexity: O(1)
- All calculations are done in constant time, independent of input size.

Space Complexity: O(1)
- Uses only a few variables, no extra data structures needed.
"""

import math # Import math module to access pi

def circle_properties(radius):
"""
Calculates area and circumference of a circle given its radius
:param radius: Radius of the circle
:return: Tuple (area, circumference) or error message for negative radius
"""
# Validate input
if radius < 0:
return "Error: Radius cannot be negative"

# Calculate area using formula π * r^2
area = math.pi * radius * radius

# Calculate circumference using formula 2 * π * r
circumference = 2 * math.pi * radius

# Return both results
return area, circumference

# Example usage / Test cases
if __name__ == "__main__":
test_radii = [5, 0, -3] # Different test cases including edge cases

for r in test_radii:
result = circle_properties(r)
print(f"Radius: {r}")
print(f"Result: {result}")
print("-" * 30)