22.4 C
New York
Thursday, November 14, 2024
HomeProgrammingPythonPython 3 Program to Calculate The Area of a Triangle

Python 3 Program to Calculate The Area of a Triangle

In this tutorial, you will learn Python 3 program to calculate the area of a triangle using Heron's formula and when two sides and an angle is known.

In this Python tutorial, you will learn about a Python 3 program to calculate the area of a triangle. Apart from that, we will also explore:

  • Python 3 program to find the area of a triangle with base and height
  • Python 3 program to find the area of a triangle using all three sides
  • Python 3 program to find the area of a triangle using the function

To understand this program, you should have basic knowledge of the following Python programming languages:

  • Syntax
  • Variables and operators
  • Python input and output

You can calculate the area of a triangle by numerous formulas depending on the metrics you know. For example, if you know the three sides of a triangle, you can easily calculate the area of a triangle using Heron’s formula.

We will see the Python code for every scenario to calculate the area of a triangle. To understand the theory, visit Mathisfun.

Also ReadSending Emails Using Python With Image And PDF Attachments.

Python 3 Program To Find The Area Of A Triangle With Base And Height

To calculate the area of a triangle whose base and height are known, you can use this formula = (Base x Height)/2.

Here’s the Python 3 program to calculate the area:

base = input('Enter the base of the triangle:');  # Taking base input from user
base = float(base);  # Converting string input data to float type
height = input('Enter the height of the triangle');  # Taking height input user
height = float(height);  # Converting string input data to float type
Area = float((base * height) / 2);  # Calculateing the area of the triangle using (Base * Height)/2
print('Area of triangle', Area);  # Outputting the area of the triangle

Output:

calculating the area of triangle in python using base and height

Python 3 Program To Calculate The Area Of A Triangle When All Three Sides Are Known

When you know all three sides of a triangle, you can calculate the area of a triangle using Heron’s formula.

First, calculate the semi-perimeter (S) by this formula – (Side 1 + Side 2 + Side 3)/2

After that, use this formula to calculate the area – √S*(S-Side1)*(S-Side2)*(S-Side3)

Here’s the Python program.

import math
Side1 = float(input('Enter the first side of the triangle = ')) #First side of the triangle
Side2 = float(input('Enter the second side of the triangle = ')) #Second side of the triangle
Side3 = float(input('Enter the third side of the triangle = ')) #Third side of the triangle
S = float(((Side1+Side2+Side3)/2)) #Calculating semi perimeter
Area = float((S*(S-Side1)*(S-Side2)*(S-Side3))) #Solving Heron's expression within square root
Area = math.sqrt(Area) #Calculating the square root to find the area 
print("Area of the triangle = ", Area) #Outputting the area of the triangle.

Output:

python program output to calculate area of a triangle using heron formula

Download Python Program.

Python 3 program to find the area of a triangle using the function

  • First, we will import the math Python library to use the math.sqrt() function to calculate the square root of any number or expression.
  • In this Python program, we will use Heron’s formula to calculate the area of a triangle.
  • Heron’s formula = √S*(S-Side1)*(S-Side2)*(S-Side3), where S is the semi-perimeter of a triangle.
  • We will define a Python function AreaTriangle(side1, side2, side3) to find the area of a triangle.

Code:

import math
def TriangleArea(firstSide, secondSide, thirdSide):
    SemiPerimeter = (firstSide + secondSide + thirdSide)/2
    Area = math.sqrt((SemiPerimeter*(SemiPerimeter-firstSide)*(SemiPerimeter-secondSide)*(SemiPerimeter-thirdSide)))
    print("Semi Perimeter of the triangle = %.2f" %SemiPerimeter);
    print("Area of the triangle is %0.2f" %Area)
TriangleArea(3, 4, 5)

Output:

Semi Perimeter of the triangle = 6.00
Area of a triangle is 6.00

Programming Exercise:

Submit your programming exercise in the comments section or mail it to codeitbro@gmail.com. A few lucky problem solvers will be featured on our Facebook pageCodeItBro – Programming Made Fun.

1. Write a Python program to calculate the area of a triangle when its two sides and the angle between them are known.

2. Make this AreaOfTriangles.py file into a Triangle Area Calculator, i.e., the program will ask users how they would like to calculate the area of a triangle.

Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
RELATED ARTICLES
0
Would love your thoughts, please comment.x
()
x