Heron's formula is a common way to calculate the area of a triangle. In this Python tutorial, you will learn how to write a simple Python program to calculate the area of a triangle.
Prerequisites to Create The Python Program to Calculate the Area of a Triangle
Algorithm to Find the Area of a Triangle Using Python
- 1st Step - Ask the user to enter the length of the three sides of the triangle.
- 2nd Step - Next, calculate the area using Heron’s formula .
- 3rd Step - At last, use the print function and display the area of the triangle on the screen.
Heron’s Formula
Area of a triangle= ?(s(s-a)*(s-b)*(s-c)) Here, s = (a+b+c)/2, and a, b, and c are the length of the three sides of the given triangle.
Python Program to Calculate the Area of a Triangle
import math
a= float(input("Enter the length of first side: "))
b= float(input("Enter the length of second side: "))
c= float(input("Enter the length of third side: "))
s= (a+b+c)/2
area = math.sqrt((s*(s-a)*(s-b)*(s-c)))
print("The area of Triangle is: {0:.2f}".format(area))
Input
Enter the length of first side: 4
Enter the length of second side: 13
Enter the length of third side: 15
Output
The area of Triangle is: 24.00
Conclusion
In this example, we have used Heron’s formula to calculate the area of the triangle. It is a straightforward program with only a few lines of code. Moreover, to enhance your Python logic-building skill, you can use some other logic to find the area of the triangle. Ensure to share them with the TGB community via the dedicated comments section below.
People are also reading:
- Program to Find Cube of a Number using Functions
- Python Program to Make a Simple Calculator
- Python Program to Find the Sum of Natural Numbers
- Program to calculate the area of a circle, a rectangle, or a triangle
- Python Program to Find the Factorial of a Number
- Program to calculate sum and average of three numbers
- Python Program to find Hash of File
- Program to print the truth table for XY+Z
- Python Program to Find the Largest Among Three Numbers
- Program in C++ and Python to calculate 3X3 Matrix multiplication
Leave a Comment on this Post