In this post, we will learn how to write a program to find the area of a triangle in python with a detailed explanation but before writing a program it is important to know the formula for finding the area of a triangle.

So let’s see the formula for finding the area of a triangle…

## Formula for Area of a Triangle

The formula is: **area = sqrt(s * (s-a) * (s-b) * (s-c))**

- where “
**s**” is the semi-perimeter of the triangle. - “
**a**“**,**“**b**“**c**“

Formula to find semi-perimeter of the triangle is as follow:

**s = (a + b + c) / 2**

From the above formula, we understood how to implement a python program to calculate the area of a triangle but before writing a program few programming concepts you have to know:

- math module (Optional)
- how to take user input in python
- Python operators

### Source Code

```
import math
a = float(input("Enter length of side a : "))
b = float(input("Enter length of side b : "))
c = float(input("Enter length of side c : "))
s = (a+b+c)/2
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
print(f"Area of a Triangle is {area:.2f}")
```

### Output

Enter length of side a : 3 Enter length of side b : 4 Enter length of side c : 5 Area of a Triangle is 6.00 |

We can also find an area of a triangle using the length of its height and base.

## Find the Area of a Triangle **with a base and height**

The formula is: **area = (1/2) * base * height**

let’s write a program using the height and base of the triangle and see the output.

### Source Code

```
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
if base > 0 and height > 0:
area = (1/2) * base * height
print(f"The area of the triangle is: {area:.2f}")
else:
print("Please enter positive numbers for base and height.")
```

### Output

Enter the base of the triangle: 12.32 Enter the height of the triangle: 43.56 The area of the triangle is: 268.33 |