Python Program to Calculate Circle Area
14 min
# 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:
- The program starts by importing the
piconstant from themathmodule. - It then prompts the user to enter the radius of the circle using the
inputfunction and converts the input to a float usingfloat(). - The area of the circle is calculated using the formula
area = pi * r^2, whereris the radius entered by the user. - The calculated area is converted to a string using
str()and concatenated with a message string. - 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
Comentarios (0) blog/es/python-program-to-calculate-circle-area
No hay comentarios aún. ¡Sé el primero en comentar!