Python Program to Find the Area of a Triangle

This is a Python Program to find the area of a triangle given all three sides.

Problem Description

The program takes three sides of a triangle and prints the area formed by all three sides.

Problem Solution

1. Take in all the three sides of the triangle and store it in three separate variables.
2. Then using the Heron’s formula, compute the area of the triangle.
3. Print the area of the triangle.
4. Exit.

Program/Source Code

Here is source code of the Python Program to find the area of a triangle given all three sides. The program output is also shown below.

import math
a=int(input("Enter first side: "))
b=int(input("Enter second side: "))
c=int(input("Enter third side: "))
s=(a+b+c)/2
area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print("Area of the triangle is: ",round(area,2))
Program Explanation

1. User must enter all three numbers and store it in separate variables.
2. First the value of s is found out which is equal to (a+b+c)/2
3. Then the Heron’s formula is applied to determine the area of the triangle formed by all three sides.
4. Then the area of the triangle is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter first side: 15
Enter second side: 9
Enter third side: 7
Area of the triangle is:  20.69
 
Case 2:
Enter first side: 5
Enter second side: 6
Enter third side: 7
Area of the triangle is:  14.7

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.