The area of a circle is calculated using the formula:
Area = π × r², where 'r' is the radius of the circle.
We will explore three different methods to calculate the area of a circle in Python programming.
This method directly computes the area using the formula.
import math
radius = float(input("Enter the radius of the circle: "))
area = math.pi * radius * radius
print(f"Area of the circle: {area:.2f}")
This method uses a function to calculate and return the area.
import math
def calculate_area(radius):
return math.pi * radius * radius
radius = float(input("Enter the radius of the circle: "))
print(f"Area of the circle: {calculate_area(radius):.2f}")
This method demonstrates recursion, though recursion is not typically needed for simple mathematical operations.
import math
def recursive_area(radius, times):
if times == 0:
return math.pi * radius * radius
return recursive_area(radius, times - 1)
radius = float(input("Enter the radius of the circle: "))
print(f"Area of the circle: {recursive_area(radius, 1):.2f}")