How to find area of a triangle in Python

Published on Aug. 22, 2023, 12:16 p.m.

To find the area of a triangle in Python, you can use the following program:

base = float(input("Enter the length of the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = 0.5 * base * height
print("The area of the triangle is", area)

In this program, we first prompt the user to enter the length of the base and height of the triangle using the input() function, and then convert the input to a float using the float() function. We then use the formula for the area of a triangle, which is 0.5 times the base times the height, to calculate the area of the triangle. Finally, we use the print() function to display the result to the user.

Note that the area variable in this program will store the area of the triangle as a float value. If you want to output the area as an integer, you can use the int() function to convert the result to an integer before printing it.

Also, note that this program calculates the area of a right-angled triangle. If you have three sides of the triangle, you can use Heron’s formula to calculate the area.

Tags:

related content