TutorialStudyMite

Area of Triangle in Python

HHimani Kohli1 min read
Beginner friendly

Track completion, mastery, and revision.

Python program to find the area of a triangle

Here, in this python program, we will learn to find the area of a triangle and display the result. The program consists of if condition where we need to find the area either by base-height formula or side formula.

Algorithm:

  1. Input the choice from the user to identify the area and display it.
  2. Using if condition, we find the area either by base-height formula or side formula.
  3. print the result/area of the triangle.
  4. Exit 

Code:

a=int(input("area with sides(1) or base-height(2) :"))
if a==2:
    b=int(input("enter the base:"))
    h=int(input("enter the height:"))
    area= float(0.5h)
    print("area is", area)
else:
    s1=float(input("enter side 1:"))
    s2=float(input("enter side 2:"))
    s3=float(input("enter side 3:"))
    s=float((s1+s2+s3)/2)
    area = (s*(s-s1)(s-s3)) ** 0.5
    print("area is", area)

Output: 

area with sides(1) or base-height(2) :2
enter the base:2
enter the height:4
area is 4.0
area with sides(1) or base-height(2) :1
enter side 1:4
enter side 2:4
enter side 3:2
area is 3.872983346207417

Finished reading?

Was this helpful?

Your feedback shapes better tutorials for everyone.