Important Python Concept

How to find the area of a triangle in Python

In this article, we will learn about how to calculate the area of a triangle in Python and display it.

General Formula To Calculate Area Of Triangle

area = (s(s-a)*(s-b)*(s-c))
Where  s =( a + b + c )/2 and a,b,c are the length of each side of a triangle.

Here we are using Heron’s formula to determine the area of the triangle. There are many other formulas to find the area of the triangle but they are used for some specific kinds of triangles only. So, to avoid this confusion we use Heron’s formula which is applicable to all triangles.

Python code to find the area of Triangle

import math

a = int(input("Enter the first side of the triangle : "))
b = int(input("Enter the second side of the triangle : "))
c = int(input("Enter the third side of the triangle : "))

s = (a + b + c) / 2
s_final = s * (s - a) * (s - b) * (s - c)
area = math.sqrt(s_final)
print("The area Of triangle is " + str(area))

Output:

Enter the first side of the triangle : 5
Enter the second side of the triangle : 6
Enter the third side of the triangle : 7
The area Of triangle is 14.696938456699069

Code Explanation:

By using the formula:

s = (a + b + c) / 2
We have calculated the semi-perimeter of the triangle
area = (s(s-a)*(s-b)*(s-c))
Here s is the semi-perimeter of the triangle and a,b,c are the length of each side of a triangle.

Conclusion

In the above article, we learned about heron’s formula and how to calculate the area of a triangle using Heron’s formula in python.

Aniket Malik

Aniket Malik

CERTIFIED TUTOR/TRAINER WITH 300+ REVIEWS

Facing difficulty with

this concept?