Python Program to Calculate The Area of The Triangle

In this Python article, we will study the program to calculate the area of a triangle. Let’s get started.

1. A Brief about Triangle and it’s Formulas

The area of a triangle can be defined as the total space occupied by the three sides of any specific triangle in a 2-dimensional plane.

The area of a right-angled triangle is simply equal to half of the base multiplied by height, which is simply Area(A) = 1/2 *( b *h ). Therefore, in order to calculate the area of a right-angled triangle, we need to know the base (b) and height (h) of it.

However, there is one more method or formula which can be used to calculate the area of any triangle. The method is using Heron’s Formula. Heron’s formula is generally used to calculate the area of any triangle.

Hence, we will discuss both the ways to calculate the area using python. Let’s implement each of them.

Some of the topics which will be helpful for understanding the program implementation better are:


2. Calculating the Area of Traingle using Heron’s Formula

For calculating the area of the triangle using Heron’s formula, we need three sides; a, b, c be the three sides of the triangle.

Let’s find out the implementation of the program.

#Python Program to calculate area of triangle using Heron's formula

s1 = float(input("Enter first side value: "))  
s2 = float(input("Enter second side value: "))  
s3 = float(input("Enter third side value: "))  
  
# semi-perimeter s  
s = (s1 + s2 + s3) / 2  
  
area_triangle = (s * (s-s1) * (s-s2) * (s-s3)) ** 0.5  
print("The area of the triangle is: ",area_triangle) 
Output
Enter first side value: 5
Enter second side value: 6
Enter third side value: 7
The area of the triangle is:  14.696938456699069

In the above program

  • We took input from the user for each of the sides.
  • Then we calculate the semi perimeter of the triangle.
  • Then we applied Heron’s Formula.

3. Calculating the Area of Right angled traingle

Let’s consider a triangle that has one right angle and the base (b) and height(h) are provided. Let’s calculate the area of this triangle.

base = int(input("Enter the base: "))
height = int(input("Enter the height: "))

#Float Division
area= (base*height)/2                  
print("\nArea of the right angled triangle is: ",area)
Output
Enter the base: 8
Enter the height: 6

Area of the right angled triangle is:  24.0

4. Conclusion

In this article, we have covered how to find the area of the triangle using Heron’s formula and also the area of any right-angled triangle.


Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x