# Python Program to Find Area of a Triangle

Python Program to Find Area of a Triangle | Here, we will discuss all possible methods to write a python program to calculate the area of the triangle. Generally, we find the area of a triangle having three sides, or the area of the right-angle triangle. The python program will calculate the area of the triangle using various methods.

Python program to find the area of the right-angle triangle

Area of Right angle triangle or Triangle whose base and height is known, their area is given by,
Area of triangle = (1/2) * base * height

Mathematically,

Inputs: base = 4
height = 5
Area of triangle = (1/2) x 4 x 5 = (1/2) x 20 = 10

Now let’s see how we can implement the area of the right-angle triangle calculator in Python.

Program description:- Write a Python program to find the area of the right-angle triangle.

This is the simplest and easiest way to calculate the area of the triangle in python. We will take base and height when declaring the variable, the value of the area will be calculated and stored in the variable, and finally, it will be displayed on the screen.

``````# python program to find the area of the right-angle triangle

# take inputs
base = float(input('Enter the base of the triangle: '))
height = float(input('Enter the height of the triangle: '))

# calculate area of triangle
area = (1/2) * base * height

# display result
print('Area of triangle = ',area)``````

Output:-

Enter the base of the triangle: 5
Enter the height of the triangle: 8
Area of triangle = 20.0

Enter the base of the triangle: 8.6
Enter the height of the triangle: 4.2
Area of triangle = 18.06

In this program, Inputs (base and height) will be provided by the user. Inputs are scanned using the input() function. input() method reads user input as a string. So, we need to convert it to float first and store it in the variable.

``````base = float(input('Enter the base of the triangle: '))
height = float(input('Enter the height of the triangle: '))``````

Now, calculate the area of the right-angle triangle using formula.

``area = (1/2) * base * height``

Then, the value of the area is displayed using the print() function.

``print('Area of triangle = ',area)``

## Area of Triangle in Python Given Three Sides

Triangle having three sides their area is given by Heron’s Formula for the area of a triangle. If a, b and c are sides of triangles, and s is semi-perimeter then from Heron’s Formula.

s = (a+b+c) / 2
Area of triangle = √(s(s-a)(s-b)*(s-c))
Where,
s is semi perimeter and a, b, c are the three sides of a triangle.

Mathematically,

Inputs: a = 3, b = 4, c = 5
s = (3+4+5)/2 = 12/2 = 6
area = √6(6-3)(6-4)*(6-5) = √36 = 6
Area of the triangle is 6

## Python Program to Find Area of Triangle

Program description:- Write a Python program to find the area of the triangle.

``````# python program to find the area of the triangle

# take inputs
a = float(input('Enter the first side of the triangle: '))
b = float(input('Enter the second side of the triangle: '))
c = float(input('Enter the third side of the triangle: '))

# must be smaller than the third side.
if (a < 0 or b < 0 or c < 0 or
(a+b <= c) or (a+c <=b) or (b+c <=a) ):
print('Not a valid triangle')

else:

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate area of triangle
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5

# display result
print('Area of triangle = %0.2f' %area)``````

Output:-

Enter the first side of the triangle: 5
Enter the second side of the triangle: 7
Enter the third side of the triangle: 9
Area of triangle = 17.41

Enter the first side of the triangle: 2
Enter the second side of the triangle: 7
Enter the third side of the triangle: 10
Not a valid triangle

Enter the first side of the triangle: 10.8
Enter the second side of the triangle: 15
Enter the third side of the triangle: 22.3
Area of triangle = 71.04

In this program, Inputs (a, b, and c) will be provided by the user. The python program will check the value of the three sides provided by the user. Because the sum of any two sides is equal or greater than the third side, then the triangle will not form.

``````if (a < 0 or b < 0 or c < 0 or
(a+b <= c) or (a+c <=b) or (b+c <=a) ):
print('Not a valid triangle')``````

Example:-

Enter the first side of the triangle: 2
Enter the second side of the triangle: 7
Enter the third side of the triangle: 10
Not a valid triangle

Then, calculate the semi-separator and area of the triangle using the formula, and the value of the area is displayed using the print() function.

## Find Area of Triangle using Math File

We can without providing root value, directly using the math library for the imported value of root and calculate the area of the circle. This supports us to use all the mathematical functions in Python programming.

``````# python program to find the area of the triangle

import math   # math file

# take inputs
a = float(input('Enter the first side of the triangle: '))
b = float(input('Enter the second side of the triangle: '))
c = float(input('Enter the third side of the triangle: '))

# must be smaller than the third side.
if (a < 0 or b < 0 or c < 0 or
(a+b <= c) or (a+c <=b) or (b+c <=a) ):
print('Not a valid triangle')

else:

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate area of triangle
area = math.sqrt(s * (s - a) * (s - b) * (s - c))

# display result
print('Area of triangle = %0.2f' %area)``````

Output:-

Enter the first side of the triangle: 25
Enter the second side of the triangle: 3
Enter the third side of the triangle: 22.5
Area of triangle = 19.65

## Using Functions

This is a different method to calculate the area of the triangle program in python. We can also take the help of function. A function is a block of code that performs a specific task.

``````# python program to find the area of the triangle

import math   # math file

def area_of_triangle(a, b, c):      #user-defined function

# must be smaller than the third side.
if (a < 0 or b < 0 or c < 0 or
(a+b <= c) or (a+c <=b) or (b+c <=a) ):
print('Not a valid trianglen')

else:
# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate area of triangle
area = math.sqrt(s * (s - a) * (s - b) * (s - c))

return area

# take inputs
a = float(input('Enter the first side of the triangle: '))
b = float(input('Enter the second side of the triangle: '))
c = float(input('Enter the third side of the triangle: '))

# display result
print('Area of triangle = %0.2f' %area_of_triangle(a, b, c))``````

Output:-

Enter the first side of the triangle: 25.8
Enter the second side of the triangle: 17
Enter the third side of the triangle: 22.5
Area of triangle = 188.49

In this program, we will be the first defined function. a, b, and c are scanned using the input() function and stored in variables. Then call the function and print the area of the triangle.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!