In this, we are going to learn different 6 Python Programs to find area of triangle. If we have three sides of a triangle by using Heron’s Formula, we can calculate the area of a triangle.
4 formulas to find the area of a triangle
#Formula to calculate Area of a Triangle
√(s(s-a)(s-b)*(s-c))
#Formula to find Perimeter of a Triangle
Perimeter = a + b + c
#Formula to find a semi-circle
s= (a+b+c)/2
#Formula to find area of triangle using coordinates
abs((0.5)*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))
1. Python Program to find the area of triangle
In this Python program we asked user to input three sides of triangle and used three sides(a,b,c) to calculate semi-circle by formula (s = (a + b + c) / 2, using Heron’s Formula (s(s-a)(s-b)*(s-c)) ** 0.5 to calculated area of triangle.
Finally, printing the result using to print() method.
Python Program
a = float(input('Please Enter length of first side: '))
b = float(input('Please Enter length of second side: '))
c = float(input('Please Enter length of third side: '))
s = (a + b + c) / 2
triangle_area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print(' triangle area is : %0.2f' %triangle_area)
Output
Please Enter length of first side: 3
Please Enter length of second side: 4
Please Enter length of third side: 5
triangle area is : 6.00
- 6 Python Programs to find area of triangle
- How to do calculate area of a circle in Python
- 4 ways to find area rectangle in Python
- Python Program to find area of triangle using coordinates
2. Python Program to Find area of triangle using Function
In this python program, we have defined a function def Cal_area_Triangle(a,b,c).We are asking users to input the three sides of the triangle and pass them to Cal_area_Triangle(a,b,c) function as arguments.
Python Program
def Cal_area_Triangle(a,b,c):
s = (a + b + c) / 2
triangle_area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('triangle area is: %0.2f' %triangle_area)
a = float(input('Please Enter length of first side: '))
b = float(input('Please Enter length of second side: '))
c = float(input('Please Enter length of third side: '))
#calling the fucntion to calculate area of trainagle
Cal_area_Triangle(a,b,c)
Output
Please Enter length of first side: 9
Please Enter length of second side: 6
Please Enter length of third side: 6
triangle area is :17.86
3. Python Program to find area of triangle Using classes
We have defined a class Triangle with a method def Triangle_area() which is used to calculate the area of the triangle Using Heron’s Formula.
- We will create an object of the class triangle obj to call the Triangle_area() method and passed enter input argument by the user to Triangle_area().
- At last printing the result.
Python Program
class Triangle:
def __init__(self,a, b, c):
self.a = a
self.b = b
self.c = c
def Triangle_area(self):
s = (self.a + self.b + self.c)/2
return (s*(s-self.a)*(s-self.b)*(s-self.c)) ** 0.5
#input three side from user
a = float(input('Please Enter length of first side: '))
b = float(input('Please Enter length of second side: '))
c = float(input('Please Enter length of third side: '))
#creating object of classTriangle
obj = Triangle(a,b,c)
#calling the class method Triangle_area()
print("Area of triangle : {}".format(obj.Triangle_area()))
Output
Please Enter length of first side: 12
Please Enter length of second side: 13
Please Enter length of third side: 14
Area of triangle : 72.30793524918272
4. Python Program to find the area of a triangle to enter base and height
The formula to find the area of a triangle with a given base and height is = (base*height)/2.
Python Program
base = float(input('Please Enter base of traingle : '))
height = float(input('Please Enter height of traingle: '))
#traigle area with base and height
triangle_area = (base*height)/2
print('calculated area of triangle is %0.2f' %triangle_area)
Output
Please Enter base of traingle : 6
Please Enter height of traingle: 9
calculated area of triangle is 27.00
5. Python Program to find area of Equilateral Triangle
Here using Python Math module,The formula to calculate the area of Equilateral Triangle is = (math.sqrt(3)/ 4)*(side * side).
Python Program
import math
side = float(input('Enter Length of any side of an Equilateral Triangle: '))
# calculate the area of equilateral triangle
Area = (math.sqrt(3)/ 4)*(side * side)
# calculate the Perimeter of equilateral triangle
Perimeter = 3 * side
print("\n calculated Area of Equilateral Triangle = %.2f" %Area)
print(" calculated Perimeter of Equilateral Triangle = %.2f" %Perimeter)
Output
Enter Length of any side of an Equilateral Triangle: 9
calculated Area of Equilateral Triangle = 35.07
calculated Perimeter of Equilateral Triangle = 27.00
6. Python program to find area of triangle using coordinates
The formulas to find the area of a triangle using co-ordinates is =
(0.5)(x1(y2-y3)+x2(y3-y1)+x3(y1-y2))
So to get the absolute value will use the abs() function.
Formual Using abs() function
abs((0.5)*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))
Python Program Example
x1 = int(input("Enter the value of x1 :"))
y1 = int(input("Enter the value of y1 :"))
x2 = int(input("Enter the value of x2 :"))
y2 = int(input("Enter the value of y2 :"))
x3 = int(input("Enter the value of x3 :"))
y3 = int(input("Enter the value of y3 :"))
Triange_Area = abs((0.5)*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))
print("calculated Area of the Triangle is :",Triange_Area)
if Triange_Area == 0:
print("A Triangle cannot be draw by the input coordinates!")
Output
Enter the value of x1 :0
Enter the value of y1 :0
Enter the value of x2 :2
Enter the value of y2 :4
Enter the value of x3 :5
Enter the value of y3 :6
calculated Area of the Triangle is : 4.0
Summary
In this, we have learned 6 Python programs to find an area of a triangle