Python Program Calculates The Area of a Circle

Python Program Calculates The Area of a Circle
Reading Time: 6 minutes

Introduction:

This Python program calculates the area of a circle based on user input for the radius. It utilizes the mathematical constant π (pi) from the math module to perform the area calculation. The program provides a user-friendly interface where the user can input the radius of the circle, and then it computes and displays the area of the circle.

Algorithm:

  1. Import the math module to access the mathematical constant pi.
  2. Define a function calculate_circle_area(radius) that takes the radius of the circle as a parameter. Inside the function:
    • Calculate the area of the circle using the formula area = math.pi * radius ** 2.
    • Return the calculated area.
  3. Define the main function main():
    • Use a try block to handle potential errors that may occur during user input.
    • Prompt the user to input the radius of the circle using the input() function and convert it to a floating-point number.
    • Check if the input radius is negative. If it is, print an error message stating that the radius cannot be negative.
    • If the radius is non-negative, call the calculate_circle_area() function with the provided radius.
    • Print the calculated area with two decimal places using the formatted string f”The area of the circle with radius {radius} is: {area:.2f}”.
    • Handle the ValueError exception if the user enters an invalid input (not a number).
  4. Use the if __name__ == “__main__”: construct to ensure that the main() function is executed only if the script is run directly (not imported as a module).

Python Code :

    
     import math
def calculate_circle_area(radius):
    area = math.pi * radius ** 2
    return area
def main():
    try:
        radius = float(input("Enter the radius of the circle: "))
        if radius < 0:
            print("Radius cannot be negative.")
        else:
            area = calculate_circle_area(radius)
            print(f"The area of the circle with radius {radius} is: {area:.2f}")
    except ValueError:
        print("Invalid input. Please enter a valid number for the radius.")
if __name__ == "__main__":
    main()

    
   

Input :

    
     Enter the radius of the circle: 60
    
   

Output :

    
     The area of the circle with radius 60.0 is: 11309.73
    
   

Explanation :

  • The program begins by importing the math module, which provides access to mathematical functions and constants, including π (pi).
  • The calculate_circle_area(radius) function calculates the area of a circle using the formula π * radius^2 and returns the calculated area.
  • The main() function serves as the entry point of the program. It guides the user through the process of providing the radius input and handles potential errors.
  • Inside the main() function, the user is prompted to input the radius using input(). The input is converted to a floating-point number using float().
  • If the input radius is negative, an error message is displayed. Otherwise, the calculate_circle_area() function is called with the radius, and the calculated area is displayed with two decimal places.
  • The program is designed to handle the ValueError exception that may occur if the user enters an invalid input (e.g., a non-numeric value).
  • Finally, the if __name__ == “__main__”: block ensures that the main() function is executed only when the script is run directly, not when it’s imported as a module in another program. This allows the program to be used as a standalone application or integrated into other code.