Python program that calculates the area of a circle based on the user input for the radius:
import math
radius = float(input("Enter the radius of the circle:
"))
area = math.pi * radius ** 2
print(f"The area of the circle is {area:.2f}.")
In this program, we first import the math module to use the
value of pi. Then, we get input from the user for the radius of the circle
using the input() function, and convert the input value to a float data type.
We then use the formula for the area of a circle, A = pi * r^2, to calculate
the area and store it in the variable area. Finally, we use the print() function
to display the result to the user, with the f-string format to round the result
to two decimal places.
You can run this program in a Python interpreter or save it
as a .py file and run it in the command line to calculate the area of a circle
for different radius values.
The output of the program would be:
Enter the radius of the
circle: 5
The area of the circle
is 78.54.
This means that the area of the circle with radius 5 is
approximately 78.54 square units. The result is rounded to two decimal places
using the f-string format. You can test this program with different radius
values to calculate the corresponding areas of circles.
No comments:
Post a Comment