×

Python program to find the area of the triangle

Python program to find the area of the triangle

This article will discuss how to find the area of a triangle in Python with all three given sides. The area of a triangle represents the total region that is enclosed by the three sides of any particular triangle.

If we know the length of three sides of a triangle, then the following mathematical formula is used to calculate the area of a triangle:

Area of a Triangle = ?(s*(s-a)*(s-b)*(s-c)) Or, Area of a Triangle = sqrt of(s*(s-a)*(s-b)*(s-c))

Here a, b, c are the three sides of a triangle, and s is the semi-perimeter that can be calculated as below:

s = (a + b + c )/ 2

Source Code

The following is a source code to calculate the area of a triangle in Python:

#Program to find the area of the triangle  
# Three sides of the triangle is a, b and c:  
 a = float(input("Enter first side: "))     
 b = float(input("Enter second side: "))   
 c = float(input("Enter third side: "))     
 # calculate the semi-perimeter    s = (a + b + c) / 2   
  # calculate the area  
 area = (s * (s - a) * (s - b) * (s - c)) ** 0.5  
 print('The area of the triangle is %0.2f' % area)  

Output

Here is the output:

Python program to find the area of the triangle

The following source code calculates the area of a triangle in Python by importing math function:

#import math function import math   
# Three sides of the triangle  
a= int(input("Enter first side: ")) 
b=int(input("Enter second side: ")) 
c=int(input("Enter third side: "))   
# calculate the semi-perimeter            
s=(a+b+c)/2 area=math.sqrt(s*(s-a)*(s-b)*(s-c))   
#print the result print("Area of the triangle is: ",round(area,2))

Output

Here is the output:

Python program to find the area of the triangle

Explanation: In this program, a user first asks to enter the length of three sides of a triangle that will be stored in variables a, b and c, respectively. Next, it will first calculate the semi-perimeter and then calculate a triangle area using the specified mathematical expression. After performing the calculation, it will store the area in a variable area, and finally, the result will be printed on the screen.


Related Topics

Continue and Pass Statements in Python

Difference between continue and pass statements in Python The tasks can be repeated and automated efficiently using loops in Python. Sometimes we have to exit from the entire loop completely, we...

2 minutes read.

Python Tricks: The Book

A Buffet of Awesome Python Features Dan Bader is the author of the book called Python Tricks . he is the owner and editor of Real Python and one of the...

3 minutes read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

How to import numy in python

How to Install NumPy in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.

Paramiko Python Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

AES CTR Python

Python Programming Language  Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

After Python, What Should I Learn

Python is a programming language used to create websites, software, and other projects. It is easy to code and easy to learn. After learning Python, there are many different directions...

4 minutes read.

SKLearn Clustering

These are ml methods thatare responsible for detecting patterns and the similarities within the data.The clustering methods are unsupervised.Here the data is clustered to form groups with the help of...

3 minutes read.

Python Dictionary Methods

Python Dictionary Methods Python has a set of built-in methods that dictionary objects can call. All the python dictionary methods are as follow: Methods Description clear() The dictionary.clear() method removes all the elements...

3 minutes read.

Captcha Code in Python with Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

os.rename() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

How to Install Numpy in PyCharm

What is PyCharm? JetBrains created the hybrid platform known as PyCharm as a Python IDE. The Python IDE PyCharm is used by some major companies, including Twitter, Facebook, Amazon, and many...

4 minutes read.

Python Project Ideas Based On Django

Introduction If you have learned Python and you are an expert in Django, then your practical skills should be excellent in this field. If you want to check your practical skills,...

9 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

How to learn python Online

Need to Learn a Programming Language 1. To get a high Paying job We know that Software Engineering is one of the top-paying professions in the world. The top criteria to be...

3 minutes read.

Pltpcolor in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.