Calculate the Area, Semi Perimeter and Perimeter of a Triangle

Python Program to Calculate the Area, Semi Perimeter and Perimeter of a Triangle

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

With an example, learn how to construct a Python program to find the Area of a Triangle, Perimeter of a Triangle, and Semi-Perimeter of a Triangle. Let’s look at the definitions and mathematics underlying Perimeter and Area of a Triangle before diving into the Python program to find Area of a Triangle.

Area of Triangle:

If we know the lengths of the three sides of a triangle, we can use Heron’s Formula to compute the area of the triangle.

Triangle Area = √((s*(s-a)*(s-b)*(s-c)))

Where

s = (a + b + c )/ 2

(Here s = semi perimeter and a, b, c are the three sides of a given_triangle)

Perimeter of a Triangle = a + b + c

Examples:

Example 1:

Input:

a = 24
b = 12
c = 22

Output:

The semiperimeter of the triangle with the sides 24 12 22 = 29.0
The perimeter of the triangle with the sides 24 12 22 = 58
The area of the triangle with the sides 24 12 22 =131.358

Example 2:

Input:

a = 5.6
b = 2.7
c = 7.9

Output:

The semiperimeter of the triangle with the sides 5.6 2.7 7.9 =8.100
The perimeter of the triangle with the sides 5.6 2.7 7.9 =16.200
The area of the triangle with the sides 5.6 2.7 7.9 =4.677

Program to Calculate the Area, Semi Perimeter and Perimeter of a Triangle

Let us calculate the area, semi perimeter and perimeter of Triangle in Python:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

1)Calculating Semi Perimeter of the given triangle

We can calculate semi perimeter using the formula

s=(a+b+c)/2 where a , b ,c are sides of the triangle

Below is the implementation:

# given sides of the triangle
a = 24
b = 12
c = 22

# calculating the semi perimeter of the given triangle by using the formula
semi_perimeter = (a + b + c) / 2
# print semi_perimeter
print("The semiperimeter of the triangle with the sides", a, b, c, "=", semi_perimeter)

Output:

The semiperimeter of the triangle with the sides 24 12 22 = 29.0

2)Calculating Perimeter of the given triangle

We can calculate perimeter using the formula

perimeter=(a+b+c) where a , b ,c are sides of the triangle

Below is the implementation:

# given sides of the triangle
a = 24
b = 12
c = 22

# calculating the  perimeter of the given triangle by using the formula
perimetr = (a + b + c)
# print perimeter
print("The perimeter of the triangle with the sides", a, b, c, "=", perimetr)

Output:

The perimeter of the triangle with the sides 24 12 22 = 58

3)Calculating Area of Triangle

When three sides are specified in this program, the area of the triangle will be determined using Heron’s formula.

If you need to calculate the area of a triangle based on user input, you can utilize the input() function.

Approach:

  • Scan the sides of the given triangle.
  • The semi-perimeter will be calculated using Heron’s formula.
  • To determine the area of the triangle, use Area = (s*(s-a)*(s-b)*(s-c)) ** 0.5.
  • Finally, print the triangle’s area

Below is the implementation:

# given sides of the triangle
a = 24
b = 12
c = 22

# calculating the  semiperimeter of the given triangle by using the formula
s = (a + b + c)/2
# calculating area of triangle with given sides
triangleArea = (s*(s-a)*(s-b)*(s-c)) ** 0.5
# print the area of thee triangle
print("The area of the triangle with the sides",
      a, b, c, "=%0.3f" % triangleArea)

Output:

The area of the triangle with the sides 24 12 22 =131.358

Related Programs: