Python Program to Calculate the Area of a Triangle


10xer.co Ad Sponsored

β“˜ Sponsored by 10xer.co

If you know the three sides of a triangle, you can use Heron’s formula to find its area12. Heron’s formula says that if a, b and c are the three sides of a triangle, then its area is A = √ [s (s-a) (s-b) (s-c)], where s is the semi-perimeter of the triangle2. The semi-perimeter is half of the perimeter, which is the sum of all sides. You can find it by s = (a + b + c)/22. For example, if a triangle has sides of 4 units, 6 units and 8 units, then its semi-perimeter is s = (4 + 6 + 8)/2 = 9 units. Then its area is A = √ [9 (9-4) (9-6) (9-8)] = √ [9 Γ— 5 Γ— 3 Γ— 1] = √135 β‰ˆ 11.62 square units


Python Code :

Let a, b and c be the 3 sides of a triangle to calculate the area of a triangle, we use the below formula :

s = (a+b+c)/2
area = √(s(s-a)*(s-b)*(s-c))

and to implement the above formula in python check the below program :

# Python Program to find the area of triangle or 

a = 7
b = 10
c = 12

# If you want to take input from the user
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('Area of the triangle is %0.2f' %area)

Output:

Area of the triangle is 34.98

The area of the traingle is calculated using the Heron’s formula.

In geometry, Heron’s formula (or Hero’s formula) gives the area A of a triangle in terms of the three side lengths a, b, c. If

s = 1/2(a+b+c)

is the semiperimeter of the triangle, then area is

area = √(s(s-a)*(s-b)*(s-c))

It is named after first-century engineer Heron of Alexandria (or Hero) who proved it in his work Metrica, though it was probably known centuries earlier.


10xer.co Ad Sponsored

β“˜ Sponsored by 10xer.co