Python Program for an Area Calculator

In this tutorial, we will discuss a Python program for an area calculator.

Before going to the program first, let us understand what is an Area Calculator .

Area Calculator:

  • Area calculator determines the area of different shapes such as a rectangle, triangles, and circles using a Python program.

Related: Python Program to Calculate the Sum of Digits

Program code for area calculator using Python

# Area Calculator in Python
import math

def area_of_rectangle(length, width):
    return length * width

def area_of_triangle(base, height):
    return 0.5 * base * height

def area_of_circle(radius):
    return math.pi * (radius ** 2)

print("Select shape to calculate area:")
print("1. Rectangle")
print("2. Triangle")
print("3. Circle")

choice = input("Enter choice(1/2/3): ")

if choice == '1':
    length = float(input("Enter the length of the rectangle: "))
    width = float(input("Enter the width of the rectangle: "))
    print(f"The area of the rectangle is {area_of_rectangle(length, width)}.")
elif choice == '2':
    base = float(input("Enter the base of the triangle: "))
    height = float(input("Enter the height of the triangle: "))
    print(f"The area of the triangle is {area_of_triangle(base, height)}.")
elif choice == '3':
    radius = float(input("Enter the radius of the circle: "))
    print(f"The area of the circle is {area_of_circle(radius)}.")
else:
    print("Invalid input")

Explanation

  1. Function Definitions: The program defines three functions: area_of_rectangle, area_of_triangle, and area_of_circle to calculate the respective areas.
  2. Main Program: The program prompts the user to select a shape and enter the necessary dimensions. Based on the user’s choice, it calculates the area of the selected shape and prints the result.

Output

Python Program for an Area Calculator

  • When you run the above program, it will prompt you to select a shape and enter the necessary dimensions.
  • After entering the dimensions, it will calculate the area of the selected shape and print the result.

Conclusion

  • In this tutorial, we learned how to create an area calculator that can calculate the area of different shapes using a Python program.
  • Understanding this concept is essential for solving various geometric problems and enhancing your programming skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *