Python Program to Calculate the Area of a Triangle

Posted in

Python Program to Calculate the Area of a Triangle
vinaykhatri

Vinay Khatri
Last updated on April 27, 2024

    Heron's formula is a common way to calculate the area of a triangle. In this Python tutorial, you will learn how to write a simple Python program to calculate the area of a triangle.

    Prerequisites to Create The Python Program to Calculate the Area of a Triangle

    Algorithm to Find the Area of a Triangle Using Python

    • 1st Step - Ask the user to enter the length of the three sides of the triangle.
    • 2nd Step - Next, calculate the area using Heron’s formula .
    • 3rd Step - At last, use the print function and display the area of the triangle on the screen.

    Heron’s Formula

    Area of a triangle= ?(s(s-a)*(s-b)*(s-c)) Here, s = (a+b+c)/2, and a, b, and c are the length of the three sides of the given triangle.

    Python Program to Calculate the Area of a Triangle

    import math
    a= float(input("Enter the length of first side: "))
    b= float(input("Enter the length of second side: "))
    c= float(input("Enter the length of third side: "))
    s= (a+b+c)/2
    area = math.sqrt((s*(s-a)*(s-b)*(s-c)))
    print("The area of Triangle is: {0:.2f}".format(area))

    Input

    Enter the length of first side: 4
    Enter the length of second side: 13
    Enter the length of third side: 15

    Output

    The area of Triangle is: 24.00

    Conclusion

    In this example, we have used Heron’s formula to calculate the area of the triangle. It is a straightforward program with only a few lines of code. Moreover, to enhance your Python logic-building skill, you can use some other logic to find the area of the triangle. Ensure to share them with the TGB community via the dedicated comments section below.

    People are also reading:

    Leave a Comment on this Post

    0 Comments