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:-
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 second side: 7
Enter third side: 6
The area of the triangle is 11.98