Python Program to Calculate the Area of a Triangle

In this tutorial, we will learn to calculate the area of a triangle using a function. In the calculating area of the triangle, we will take three sides as input from a user. The  Python concept required to find the area of a triangle is:

  • math library
  • input function
  • sqrt function

The formula of Area of a Triangle

To find the area of a triangle where a,b,c are the three sides of a given triangle :

  • find semi-perimeter of a triangle
    s = (a+b+c)/2
  • the formula for the area of a triangle
    Area =   (s*(s-a)*(s-b)*(s-c))**0.5

NOTE: The formula for the area of an equilateral triangle, isosceles triangle, and the right-angled triangle is same when three sides are given

Python program to find the Area of a triangle

 

# Python Program to find Area of a Triangle using Functions
import math

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

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

    print(" The Area of a Triangle is %0.2f" %area)

#input of a sides    
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))

Area_of_triangle(a,b,c)

In a program, we used math library to find the square root in the formula of area. The Area_of_triangle function is written to find the area of a triangle in which initially it calculates semi-perimeter. Then using Heron’s formula we calculate the area of a triangle. So to calculate area we take input from the user and on that input, we perform a function operation. At last, we call a function to operate on a given input.

 

OUTPUT:

Enter first side: 4
Enter second side: 3
Enter third side: 5
The Area of a Triangle is 6.00

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *