Program to Calculate the Area of a Circle in Python

Calculating the Area of a Circle

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.

Method 1: Using Basic Arithmetic

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}")
            
Input: 5.0
Output: Area of the circle: 78.54

Method 2: Using a Function

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}")
            
Input: 7.0
Output: Area of the circle: 153.94

Method 3: Using Recursion

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}")
            
Input: 4.0
Output: Area of the circle: 50.27