Home » Python Program to Find Area of Triangle Using Function

# Python Program to Find Area of Triangle Using Function

Learn about Python Program to Find Area of Triangle Using Function in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

#### Python Program to Find Area of Triangle Using Function

``````# program to find the area of the triangle using function

# math module
import math

def area_of_triangle(a, b, c):
# sum of two sides 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:
# calculating the semi-perimeter
s = (a + b + c) / 2

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

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

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

Output:

```first side of the triangle: 3
second side of the triangle: 4
third side of the triangle: 5
Area of triangle = 6.00```

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.