Python program to find the area of a triangle

Learn tho calculate the area of a triangle in Python.

Python program to find the area of a triangle

According to Heron’s formula, if a, b and c are the three sides of a triangle, the area of the triangle is:-

Area of a triangle using Heron's formula

where s is the semi-perimeter of the triangle.

Method 1

Code

a = 10
b = 12
c = 13

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

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of our triangle is %0.2f' %area)

Output

The area of our triangle is 57.00

Method 2 - User Input

Code

a = float(input('Enter 1st side: '))
b = float(input('Enter 2nd side: '))
c = float(input('Enter 3rd side: '))

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

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of our triangle is %0.2f' %area)

Output

Enter 1st side: 10
Enter 2nd side: 12
Enter 3rd side: 13
The area of our triangle is 57.00

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe