by BehindJava

Write a program to find the area of a triangle in Python

Home » python » Write a program to find the area of a triangle in Python

In this tutorial we are going to learn about finding the area of triangle in Python.

Mathematical formula:

Area of a triangle = (s(s-a)(s-b)*(s-c))-1/2

# inputs from the user
a = float(input('Enter length of first side: '))
b = float(input('Enter length of second side: '))
c = float(input('Enter length of third side: '))
 
# Calculate the semi-perimeter
s = (a + b + c) / 2
 
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

Calculate the Area of a Triangle using function

def areaOfTriangle(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('The area of a traingle is %0.2f' %area)
 
 
# Initialize first side of traingle
a = 21
# Initialize second side of traingle
b = 23
# Initialize third side of traingle
c = 35

areaOfTriangle(a,b,c)

Calculate area of triangle using Math file

# Python Program to find the area of triangle
# import math file
import math

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

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

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

# display result
print('The area of the triangle is %0.2f' %area)