How to Write a Python Program to Calculate the Area of a Circle from User Input?

Estimated read time 2 min read

To write a Python program that calculates the area of a circle based on user input, you can use the following code:

import math

# Get the radius from the user
radius = float(input("Enter the radius of the circle: "))

# Calculate the area
area = math.pi * radius**2

# Print the result
print(f"The area of the circle is: {area}")

In the code above:

  1. The math module is imported to access the value of pi (math.pi) and perform mathematical operations.
  2. The input() function is used to prompt the user to enter the radius of the circle. The float() function is used to convert the user’s input into a floating-point number.
  3. The area of the circle is calculated using the formula math.pi * radius**2.
  4. The result is printed to the console using the print() function. The f-string formatting is used to display the area value within the string.

When you run the program, it will prompt you to enter the radius of the circle. After you provide the input, it will calculate and display the area of the circle.

Note that the radius should be a positive number. You may want to consider adding input validation or error handling to ensure that the user enters a valid radius.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply