Python program to find area of triangle; Through tutorial, i am going to show you how to find or calculate area of triangle in python using function.
The area of a polygon is the number of square units inside that polygon. A triangle is a three-sided polygon. We will look at several types of triangles in this lesson. To find the area of a triangle, multiply the base by the height, and then divide by 2.
In this tutorial, i will write a python programs to calculate or find area of triangle using coordinates and function.
Python Program to Find or Calculate Area of Triangle
- Find Area of Triangle Formula
- Python program to find area of triangle using coordinates
- Python Program to Find the Area of a Triangle using Function
Find Area of Triangle Formula
First of all, you must know the length of three sides of a triangle. So you can calculate the area of a triangle using Heron’s Formula, which is given below:
Area of a Triangle = √(s*(s-a)*(s-b)*(s-c))
Where s = (a + b + c )/ 2 (Here s = semi perimeter and a, b, c are the three sides of a triangle)
Perimeter of a Triangle = a + b + c
Python program to find area of triangle using coordinates
See the following steps to calculate area of triangle in python:
- Allow the user to input the triangle sides.
- Calculate the Perimeter of Triangle using the formula P = a+b+c.
- Calculating the semi perimeter using the formula (a+b+c)/2.
- Calculating the Area of a triangle using Heron’s Formula: (s*(s-a)*(s-b)*(s-c)) ** 0.5.
- Print the Area of a triangle.
# Python Program to find Area of a Triangle a = float(input('Please Enter the First side of a Triangle: ')) b = float(input('Please Enter the Second side of b Triangle: ')) c = float(input('Please Enter the Third side of c Triangle: ')) # calculate the Perimeter Perimeter = 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("\n The Perimeter of Traiangle = %.2f" %Perimeter); print(" The Semi Perimeter of Traiangle = %.2f" %s); print(" The Area of a Triangle is %0.2f" %Area)
After executing the python program, the output will be:
Please Enter the First side of a Triangle: 5 Please Enter the Second side of b Triangle: 4 Please Enter the Third side of c Triangle: 3 The Perimeter of Traiangle = 12.00 The Semi Perimeter of Traiangle = 6.00 The Area of a Triangle is 6.00
Python Program to Find the Area of a Triangle using Function
Use the following steps to create python program to calculate area of triangle using function
- Import math module.
- Defined the function with three arguments using def keyword.
- Calculate the perimeter and Area of a triangle inside functions using math.sqrt() and with Heron’s Formula.
- Allow the User to input the sides of triangle.
- Print the Perimeter and Area of a triangle.
# Python Program to find Area of a Triangle using Functions import math def AreaOfTriangle(a, b, c): # calculate the Perimeter Perimeter = a + b + c # calculate the semi-perimeter s = (a + b + c) / 2 # calculate the area Area = math.sqrt((s*(s-a)*(s-b)*(s-c))) print("\n The Perimeter of Traiangle = %.2f" %Perimeter); print(" The Semi Perimeter of Traiangle = %.2f" %s); print(" The Area of a Triangle is %0.2f" %Area) a = float(input('Please Enter the First side of a Triangle: ')) b = float(input('Please Enter the Second side of b Triangle: ')) c = float(input('Please Enter the Third side of c Triangle: ')) AreaOfTriangle(a, b, c)
After executing the python program, the output will be:
Please Enter the First side of a Triangle: 10 Please Enter the Second side of b Triangle: 15 Please Enter the Third side of c Triangle: 20 The Perimeter of Traiangle = 45.00 The Semi Perimeter of Traiangle = 22.50 The Area of a Triangle is 72.62