Home Tutorials Python Programs Area Calculator
Area Calculator

Area Calculator


Topic: Geometric Area Calculator

Description: This program provides an interactive command-line menu where a user can select a geometric shape (Square, Rectangle, Circle, or Triangle) and input its dimensions to calculate the precise surface area.

Why: Building an area calculator helps beginners understand how to accept user inputs, convert data types (from strings to floats), and use conditional control structures to execute completely different mathematical formulas based on a user's choice.


Program Code

Copy and run the following Python code to see the calculator in action:

def calculate_area():
    print("=============================")
    print("   GEOMETRIC AREA CALCULATOR ")
    print("=============================")
    print("1. Square")
    print("2. Rectangle")
    print("3. Circle")
    print("4. Triangle")
    
    # Get user selection
    choice = input("\nEnter the number of your choice (1-4): ")
    
    if choice == "1":
        # Area of a Square = side * side
        side = float(input("Enter the length of a side: "))
        area = side * side
        print(f"\nThe area of the square is: {area:.2f}")
        
    elif choice == "2":
        # Area of a Rectangle = length * width
        length = float(input("Enter the length: "))
        width = float(input("Enter the width: "))
        area = length * width
        print(f"\nThe area of the rectangle is: {area:.2f}")
        
    elif choice == "3":
        # Area of a Circle = pi * r^2
        pi = 3.14159
        radius = float(input("Enter the radius of the circle: "))
        area = pi * (radius ** 2)
        print(f"\nThe area of the circle is: {area:.2f}")
        
    elif choice == "4":
        # Area of a Triangle = 0.5 * base * height
        base = float(input("Enter the base length: "))
        height = float(input("Enter the height: "))
        area = 0.5 * base * height
        print(f"\nThe area of the triangle is: {area:.2f}")
        
    else:
        print("\nInvalid selection! Please run the program again and select options 1 to 4.")

# Execute the program
calculate_area()

Expected Console Output Examples

Example 1 (Selecting a Rectangle):

=============================
   GEOMETRIC AREA CALCULATOR 
=============================
1. Square
2. Rectangle
3. Circle
4. Triangle

Enter the number of your choice (1-4): 2
Enter the length: 12.5
Enter the width: 4

The area of the rectangle is: 50.00

Example 2 (Selecting a Circle):

Enter the number of your choice (1-4): 3
Enter the radius of the circle: 7

The area of the circle is: 153.94

Core Syntax & Concept Breakdown

  • input() Typecasting: The raw input() function captures values as strings (text). We wrap it in float() (e.g., float(input(...))) so Python can perform mathematical calculations on decimal numbers.
  • Exponent Operator (**): In Python, raising a number to a power uses two asterisks. radius ** 2 means $radius^2$.
  • String Formatting (:.2f): Inside the f-string, {area:.2f} instructs Python to display the float result rounded cleanly to exactly 2 decimal places.

🏋️ Test Yourself With Exercises

Take our quiz on Area Calculator to test your knowledge.

Browse Quizzes »