In this simple python program, we need to find the area of a triangle with three sides. It's a beginner-level python program.
To understand this example, you should have knowledge of the following Python programming topics:
This is a simple python program to find the area of a Triangle. A Triangle is a shape with three sides. To find the area of a triangle, we use Heron's formula. Heron's formula is defined as the square root of (s*(s-a)*(s-b)*(s-c)). Here
Here to apply this formula, we have to find the semi perimeter 's'. For that, we use the formula s = (a + b + c) / 2 then we apply the semi perimeter in the heron's formula We use the Arithmetic operators in python to do it. To apply this logic in this beginner python program, we have to accept the sides of the triangle from the user and apply the semi perimeter formula. Then with that semi perimeter, we use heron's formula. And print the result using the
STEP 1: Accept the sides of the triangle from the user using the input function in python programming and convert that python string into float using float datatype.
STEP 2: Calculate the semi perimeter using the formula (a + b + c) / 2 where a, b, c are sides of the triangle.
STEP 3: Calculate the area of triangle using heron's formula "(s*(s-a)*(s-b)*(s-c)) ** 0.5 " where 0.5 is used instead of square root in python program.
STEP 4: print the result as the triangle area is using a 0.2 precision float data type.
a = float(input('Enter the first side: ')) b = float(input('Enter the second side: ')) c = float(input('Enter the third side: ')) s = (a + b + c) / 2 # semi perimeter calculation in python area = (s * (s - a) * (s - b) * (s - c)) ** 0.5 # heron's formula apply to find area print ('The area of the triangle is %0.2f' % area)
Enter the first side: 20 Enter the second side: 20 Enter the third side: 20 The area of the triangle is 96.82