Program to find area of triangle in Python
In this post, you will learn how to find the area of triangle using the Python programming language. There are several ways to calculate the area of a triangle. Here, we will discuss all possible methods to write a Python program to calculate the area of a triangle.
Area of Triangle
If we know the size of three sides of a triangle, then we can easily calculate the area of a triangle using Heron's Formula.
Area of a Triangle = √(s*(s-a)*(s-b)*(s-c))
Here, s is the semi-perimeter, and a, b, and c are the three sides of a triangle. The semi-perimeter is calculated as follows:
s = (a + b + c)/2
Python program to calculate the Area of Triangle
The following Python program allows a user to enter the length of all three sides of a triangle. We are storing them in three variables, a, b, and c. Then, using these values, we calculate the semi-perimeter of the triangle. Hence, the area of the triangle using Heron's Formula is calculated as-
# Python Program to find the area of triangle
# inputs from the user
a = float(input('Enter length of first side: '))
b = float(input('Enter length of second side: '))
c = float(input('Enter length of third side: '))
# Calculating the semi-perimeter
s = (a + b + c) / 2
# Calculating the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
Output of the above code -
Enter length of first side: 23
Enter length of second side: 53
Enter length of third side: 31
The area of the triangle is 135.49
Python program to calculate the Area of Triangle using function
In the given Python program, we have defined a function to calculate the area of the triangle. We have provided all the values of the three sides of the triangle. We will pass these three values to the function areaOfTriangle() to calculate the area of a triangle in Python.
def areaOfTriangle(a,b,c):
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
print('The area of a traingle is %0.2f' %area)
# Initialize first side of traingle
a = 21
# Initialize second side of traingle
b = 23
# Initialize third side of traingle
c = 35
areaOfTriangle(a,b,c)
Output of the above code -
The area of a traingle is 232.93
Calculate area of triangle using Math file
Here, we have used the math module to directly calculate the area of the triangle. First, we have imported the math library that allows us to use the mathematical functions. Then we allow a user to enter three sides of the triangle. We are using these values and calculating the semi-perimeter and the area of the triangle using Heron's Formula.
# Python Program to find the area of triangle
# import math file
import math
# inputs from the user
a = float(input('Enter length of first side: '))
b = float(input('Enter length of second side: '))
c = float(input('Enter length of third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area of triangle
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
# display result
print('The area of the triangle is %0.2f' %area)
Output of the above code -
Enter length of first side: 53
Enter length of second side: 55
Enter length of third side: 85
The area of the triangle is 1415.41
Python program to calculate the area of right triangle
This Python program allows the user to enter the width and height of the right angled triangle. Using the entered values, we can calculate the area of the right-angled triangle.
# Python Program to find
# the area of right angled triangle
# import math file
import math
# inputs from the user
width = float(input('Enter the width of triangle: '))
height = float(input('Enter the height of triangle: '))
# calculate the area
area = 0.5 * width * height
# display result
print('The area of right triangle is %0.2f' %area)
Output of the above code -
Enter the width of triangle: 52
Enter the height of triangle: 23
The area of right triangle is 598.00
Related Articles
Python program to multiply two numbersRemove last element from list Python
How to add background image in Python Tkinter
Find square of a number in Python
Sum of even numbers in Python
Find square of a number in Python
Sum of n numbers in Python using while loop
Sum of n numbers in python using for loop
Python program to find area of circle
Program to find area of rectangle in Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Reverse a number in Javascript
HTML open link in new tab
Python program to bouncing ball in Pygame
Python iterate list with index
Python add list to list
Python random choice
Calculate a Percentage in Python
Remove character from string Python
Python raise keyword