How to calculate the area of a Triangle in Python
In this program, we wiil learn how to calculate the area of a triangle in python by user input. that is, we will ask the user to enter the base and the height of the triangle to perform the calculation.
The area of a triangle is calculated by dividing the base by 2 and multiply the result by the height. That's, half base times height
But before that, we first learn how to calculate the area of the triangle without taking an input from the user.
print('---------------------------WELCOME TO CODE WITH PETASCO BLOG----------------------')
print('=============================AREA OF A TRIANGLE CALCULATOR ===============================')
base = 12
height = 5
# calculating the area of a triangle
area = (base / 2) * height
print('THE AREA OF THE TRIANGLE IS: ' + str(area))
Calculating the Area of a Triangle with User Input
print('---------------------------WELCOME TO CODE WITH PETASCO BLOG----------------------')
print('=============================AREA OF A TRIANGLE CALCULATOR ===============================')
# taking the base from the user
base = float(input('ENTER THE BASE OF THE TRIANGLE: '))
# taking the height from the user
height = float(input('ENTER THE HEIGHT OF THE TRIANGLE: '))
# calculating the area of a triangle
area = (base / 2) * height
print('THE AREA OF THE TRIANGLE IS: ' + str(area))
0 Comments