Python Program Calculates the Area of a Triangle

Python Program Calculates the Area of a Triangle
Reading Time: 3 minutes

Introduction:

This Python program calculates the area of a triangle. The user is prompted to enter the lengths of the three sides of the triangle, and the program uses Heron’s formula to compute the area. Heron’s formula states that the area of a triangle with sides of length a, b, and c is given by:

area = √(s(s-a) (s-b) (s-c))

where s is the semi perimeter of the triangle, defined as (a + b + c) / 2.

Algorithm:

  1. Read the lengths of the three sides of the triangle from the user.
  2. Calculate the semi perimeter, s, using the formula (a + b + c) / 2.
  3. Calculate the area using Heron’s formula: area = √(s(s-a) (s-b) (s-c)).
  4. Print the calculated area.

Program:

				
					import math

# Read the lengths of the sides from the user
a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
c = float(input("Enter the length of side c: "))

# Calculate the semi perimeter
s = (a + b + c) / 2

# Calculate the area using Heron's formula
area = math.sqrt(s * (s - a) * (s - b) * (s - c))

# Print the area
print("The area of the triangle is:", area)



				
			

Input :

				
					Enter the length of side a : 5
Enter the length of side b : 6
Enter the length of side c : 7
				
			

Output :

				
					The Area of The Triangle is : 14.696938456699609
				
			

In this example, the user enters the lengths of the sides as 5, 6, and 7. The program calculates the area using Heron’s formula and displays the result as approximately 14.70.