Learn about Python Program to Find Area of Triangle using Math Module 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 Math Module
# python program to find the area of the triangle
# math module
import math
# input sides
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: '))
# checking triangle conditions
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:
# calculating the semi-perimeter
s = (a + b + c) / 2
# area of triangle
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
# print result
print('Area of triangle = %0.2f' %area)
Output:
Enter the first side of the triangle: 3 Enter the second side of the triangle: 4 Enter the 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.
Similar Code : Python Program to Find Area of Triangle Given Three Sides