Home » Python program to find area of a triangle (given all sides)

Python program to find area of a triangle (given all sides)

Python program to find area of a triangle (given all sides)

Hello everybody, welcome back! Here we learn how to find the area of a triangle when all the 3 sides of it are given.

First, we calculate semi-perimeter s as sum of all sides divided by 2 then calculate the area using the formula (s*(s-a)*(s-b)*(s-c)) ** 0.5

Program on Github

Formulae:

Semi-perimeter  : s = (a + b + c) / 2

Area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

You can also watch the video on YouTube here.

Task :

To find the area of a triangle when all 3 sides of a triangle are given.

Approach :

  • Read three sides(a, b, c) of a triangle using input()orraw_input().
  • We calculate the semi-perimeter s as sum of all three sides divided by 2. s = ( a + b + c ) / 2
  • After calculating semi-perimeter, we calculate is area as square root of (s*(s-a)*(s-b)*(s-c))
  • For finding square root we can use a function called sqrt from math module or by using exponential operation, we discussed about both sqrt function and using exponential operation
  • We use exponential operation here to calculate area. area =  (s*(s-a)*(s-b)*(s-c)) ** 0.5
  • Finally, print the result.

Program on Github

Area of Triangle – Code Visualization

Program :

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

s = (a + b + c) / 2

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

Output :

Area of a triangle when 3 sides are given - programminginpython.com
Area of a triangle – programminginpython.com
Area of a triangle when 3 sides are given - programminginpython.com
Area of a triangle – programminginpython.com

Program on Github

Course Suggestion

Want to learn python with strong fundamentals? If yes, I strongly suggest you to take the course below.
Course: Fundamentals of Programming in Python

 

Online Python Compiler

Leave a Reply

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