python program to find area of triangle

In this example we'll find area of triangle with user inputted side values. It will ask user to input value of each side and calculate area of triangle.

In this example we'll find area of triangle with user inputted side values. It will ask user to input value of each side and calculate area of triangle.

To calculate area of triangle, we will use below formula :

    
s = (a+b+c)/2
area = √(s(s-a)*(s-b)*(s-c))
    

Here a, b and c are three sides of a triangle.

Example :

    
a = float(input('Enter first side value : '))
b = float(input('Enter second side value: '))
c = float(input('Enter third side value: '))
  
s = (a + b + c) / 2
  
# calculate the area of triangle
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
    

Output :

    
Enter first side value : 10
Enter second side value: 5
Enter third side value: 7
The area of the triangle is 16.25
    

Explanation :

We take the input as three sides of a triangle and store them in three variables a, b, and c. Then, we have calculated the semi-perimeter of the triangle and put this value in the triangle's area formula.


Share your thoughts

Ask anything about this examples