Area of Triangle in Python
Written by
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:
- Input the choice from the user to identify the area and display it.
- Using if condition, we find the area either by base-height formula or side formula.
- print the result/area of the triangle.
- 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.5bh)
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-s2)(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