Python program to calculate the area of triangle

In this example, we will write a simple program that calculates an area of a triangle using operators in Python. To better understand this example, make sure you have knowledge of the following tutorials:-

The area of the triangle can be calculated using the basic formula:-

Here, a, b and c are the sides of the trianlge.

s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

here, s is the semi parameter.

Source Code:

a = 4
b = 7
c = 6

s = (a + b + c) / 2

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)

The output of the above program is:-

The area of the triangle is 11.98
The square root operation is performed by using the exponent operator ( ** ) to 0.5.

Example:- Use user input to calculate the area of a triangle

The above program can be modified to take input from the user and calculate the area:-

a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))

s = (a + b + c) / 2

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)

The output of the above program is:-

Enter first side: 4
Enter second side: 7
Enter third side: 6
The area of the triangle is 11.98
SHARE Python program to calculate the area of triangle

You may also like...

Leave a Reply

Your email address will not be published.

Share