Mathematically, area of triangle is defined as Region occupied by three lines which are connected with each other consequently. Area of a triangle can also be defined as under root of (s * (s – a) * (s – b) * (s – c)) where s is parameter of triangle while a, b, c are three sides. This way of calculating area of a triangle is called Heron’s Formula and it’s used for calculating if in case length of sides of triangle is given.
Quite Simple, let’s see how this mathematical logic can be moulded as some Python Algorithm.
Area of Triangle
- Ask user for three sides of triangle as variables a, b, c
- Calculate perimeter of triangle as s = (a + b + c) / 2
- Multiply perimeter of triangle with number 0.5 which will return Area of Triangle
# Three sides of the triangle is a, b and c: a = float(input('Enter first side: ')) b = float(input('Enter second side: ')) c = float(input('Enter third side: ')) # calculate the semi-perimeter of triangle s = (a + b + c) / 2 # calculate the area area_of_triangle = (s*(s-a)*(s-b)*(s-c)) ** 0.5 print('The area of the triangle is =>', area_of_triangle)
Output of Above Code
Enter first side: 3 Enter second side: 4 Enter third side: 5 The area of the triangle is => 6.0