How do you find the area of a triangle with 3 sides in python?

If a, b and c are three sides of a triangle. Then,

s = (a+b+c)/2
area = √(s(s-a)*(s-b)*(s-c))

Source Code

# Python Program to find the area of triangle

a = 5
b = 6
c = 7

# Uncomment below to take inputs from the user
# 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

The area of the triangle is 14.70

In this program, area of the triangle is calculated when three sides are given using Heron's formula.

If you need to calculate area of a triangle depending upon the input from the user, input() function can be used.

This is a Python Program to find the area of a triangle given all three sides.

Problem Description

The program takes three sides of a triangle and prints the area formed by all three sides.

Problem Solution

1. Take in all the three sides of the triangle and store it in three separate variables.
2. Then using the Heron’s formula, compute the area of the triangle.
3. Print the area of the triangle.
4. Exit.

Program/Source Code

Here is source code of the Python Program to find the area of a triangle given all three sides. The program output is also shown below.

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

Program Explanation

1. User must enter all three numbers and store it in separate variables.
2. First the value of s is found out which is equal to (a+b+c)/2
3. Then the Heron’s formula is applied to determine the area of the triangle formed by all three sides.
4. Then the area of the triangle is printed.

Runtime Test Cases

 
Case 1:
Enter first side: 15
Enter second side: 9
Enter third side: 7
Area of the triangle is:  20.69
 
Case 2:
Enter first side: 5
Enter second side: 6
Enter third side: 7
Area of the triangle is:  14.7

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

How do you find the area of a triangle with 3 sides in python?

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

In this python tutorial, you will learn about how to find area of a triangle in Python, the Python program to find the area of a triangle and, also we will check:

  • Python program to find the area of a triangle
  • Python program to calculate the area of a triangle using function
  • Python program to find the area of a triangle given all three sides
  • Python program to find the area of a triangle using class
  • Python program to find the area of a triangle when base and height are given
  • Python program to find the area of a triangle using function when base and height are given

Let see python program to find the area of a triangle.

  • In this example, we will ask the user to enter the length of three sides of a triangle.
  • We will use Heron’s formula to calculate the semi-perimeter.
  • And Area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 to calculate the area of the triangle.
  • At last, print the area of the triangle to get the output.

Example:

a = float(input('Enter the length of first side: '))  
b = float(input('Enter  the length of second side: '))  
c = float(input('Enter  the length of 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)

You can refer to the below screenshot to see the output for the python program to find the area of a triangle.

How do you find the area of a triangle with 3 sides in python?
Python program to find the area of a triangle

The above code we can use to find area of a triangle in Python.

Also, read, Python program to find the area of square.

Python program to calculate area of a triangle using function

Now, we will see python program to calculate the area of a triangle using function.

  • Firstly, we will import math module which will allow us to use the mathematical function like math.sqrt function.
  • Now, we will define the function with three arguments using the def keyword as def Areaoftriangle(a, b, c).
  • We will use Heron’s formula to calculate the semi-perimeter.
  • And Area = math.sqrt((s*(s-a)*(s-b)*(s-c)))to calculate the area of the triangle.
  • The user will enter the three sides of the triangle a,b,c and we will pass those values to function arguments to calculate the area.
  • At last, print is used to get the output.

Example:

import math
def Areaoftriangle(a, b, c):
    Perimeter = a + b + c
    s = (a + b + c) / 2
    Area = math.sqrt((s*(s-a)*(s-b)*(s-c)))
    print("The perimeter of traiangle = %.2f" %Perimeter);
    print("The Semi Perimeter of traiangle = %.2f" %s);
    print("The Area of a triangle is %0.2f" %Area)
Areaoftriangle(8, 9, 10)

You can refer to the below screenshot to see the output for python program to calculate the area of a triangle using function.

How do you find the area of a triangle with 3 sides in python?
Python program to calculate the area of a triangle using function

The above code, we can use to calculate area of a triangle using function in Python.

You may like, Python program to find an area of a rectangle.

Python program to find area of a triangle given all three sides

Here, we will see python program to find the area of a triangle given all three sides

  • Firstly, we will import math module which will allow us to use the mathematical function like math.sqrt function.
  • We will ask the user to enter the length of three sides of a triangle and it will be stored in a separate variable.
  • We will use Heron’s formula for calculating the semi-perimeter which is s=(a+b+c)/2.
  • And Area = math.sqrt(s*(s-a)*(s-b)*(s-c)) to calculate the area of the triangle.
  • At last, print the area of the triangle to get the output.

Example:

import math
a=int(input("Enter the length of first side: "))
b=int(input("Enter the length of second side: "))
c=int(input("Enter the length of third side: "))
s=(a+b+c)/2
Area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print("Area of the triangle is: ",round(Area,2))

You can refer to the below screenshot to see the output for the python program to find the area of a triangle given all three sides.

How do you find the area of a triangle with 3 sides in python?
Python program to find the area of a triangle given all three sides

This is the python program to find area of a triangle given all three sides.

Check out, How to calculate area of a circle in Python?

Python program to find area of a triangle using class

Let’s see python program to find the area of a triangle using class

  • We will create a class called class sides.
  • Another class called class A(sides) it has a method called def area(self) return (s*(s-self.a)*(s-self.b)*(s-self.c)) ** 0.5 which is the area of the class.
  • An object for the class is created as obj = A(10,12,14).
  • By using the object, the method area() is called.
  • At last, print the area of a rectangle to see the output.

Example:

class sides:
    def __init__(self,a, b, c):
        self.a = a
        self.b = b
        self.c = c
class A(sides):
    def area(self):
        s = (self.a + self.b + self.c)/2
        return (s*(s-self.a)*(s-self.b)*(s-self.c)) ** 0.5
obj = A(10,12,14)
print("Area of triangle : {}".format(obj.area()))

You can refer to the below screenshot to see the output for the python program to find the area of a triangle using class.

How do you find the area of a triangle with 3 sides in python?
Python program to find the area of a triangle using class

Also, read, How to calculate simple interest in Python?

Python program to find area of a triangle when base and height are given

Now, we will see python program to find the area of a triangle when base and height are given

  • In this example, we will ask the user to enter the base and height of a triangle.
  • To calculate the area of a triangle we will use the formula Area = (b * h)/2.
  • At last, print the area of the triangle to get the output.

Example:

b = float(input('Enter the base of a triangle: '))  
h = float(input('Enter the height of a triangle: '))  
Area = (b * h)/2
print('The area of the triangle is %0.2f' %Area)

You can refer to the below screenshot to see the output for the python program to find the area of a triangle when base and height are given.

How do you find the area of a triangle with 3 sides in python?
Python program to find the area of a triangle when base and height are given

Read: PdfFileReader Python example

Python program to find area of a triangle using function when base and height are given

Here, we will see a python program to find the area of a triangle using function when base and height are given

  • In this example, we will define the function as def Area(base, height).
  • Here, Area(10, 15) is the value passed to the function argument.
  • At last, it will return the area of the triangle and print the output.

Example:

def Area(base, height):
   return 0.5 * base * height
print(Area(10, 15))

You can refer to the below screenshot to see the output for the python program to find the area of a triangle using function when base and height are given.

How do you find the area of a triangle with 3 sides in python?

You may like the following Python tutorials:

  • Python program to print pattern
  • How to print factorial of a number in Python
  • How to swap two numbers in Python

In this Python tutorial, we have learned about the Python program to find the area of a triangle. Also, we covered these below topics:

  • Python program to find the area of a triangle
  • Python program to calculate the area of a triangle using function
  • Python program to find the area of a triangle given all three sides
  • Python program to find the area of a triangle using class
  • Python program to find the area of a triangle when base and height are given
  • Python program to find the area of a triangle using function when base and height are given

How do you find the area of a triangle with 3 sides in python?

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.

How do you find the area of a triangle with 3 points in Python?

Using the formula = abs((0.5)(x1(y2-y3)+x2(y3-y1)+x3(y1-y2))) to find triangle area. Finally calling the function triangeArea() and print the result to the output.

How do you find the area of a triangle with 3 sides?

If the sides of a triangle are given along with an included angle, the area of the triangle can be calculated with the formula, Area = (ab × sin C)/2, where 'a' and 'b' are the two given sides and C is the included angle. This is also known as the "side angle side " method.

How do you find the area in Python?

Python program to find area and perimeter of a rectangle.
Firstly, we will take input from the user for length and breadth using the input() function..
Now, we will calculate the area of a rectangle by using the formula Area = w * h..
Next, we are calculating the perimeter of a rectangle Perimeter = 2 * (w + h).

Is triangle program in Python?

# Validity of Triangle given sides # Function definition to check validity def is_valid_triangle(a,b,c): if a+b>=c and b+c>=a and c+a>=b: return True else: return False # Reading Three Sides side_a = float(input('Enter length of side a: ')) side_b = float(input('Enter length of side b: ')) side_c = float(input('Enter ...