BysMax

Python Program to Calculate Circle Area

Authors
14 min read
·
Published on
# 3. Write a Python program which accepts the radius of a circle from the user and compute the area. (area = pi * r^2)
from math import pi

r = float(input("Enter the radius of the circle: "))
area = str(pi * r**2)
print("The area of the circle with radius " + str(r) + " is " + area)

Explanation:

  1. The program starts by importing the pi constant from the math module.
  2. It then prompts the user to enter the radius of the circle using the input function and converts the input to a float using float().
  3. The area of the circle is calculated using the formula area = pi * r^2, where r is the radius entered by the user.
  4. The calculated area is converted to a string using str() and concatenated with a message string.
  5. Finally, the message string containing the radius and calculated area is printed to the console.

Usage:

When you run this program, it will prompt you to enter the radius of the circle. For example:

Enter the radius of the circle: 5

After entering the radius, the program will output the calculated area:

The area of the circle with radius 5.0 is 78.53981633974483