codescracker


python

Python Program to Calculate Area of Triangle



« Previous Program Next Program »


Calculate Area of Triangle in Python

To calculate area of a triangle in python, you have to ask from user to enter length of first, second, and third side to calculate and print area of that triangle on the output screen as shown in the program given below.

Python Programming Code to Calculate Area of Triangle

Following python program ask from user to enter length of all the three sides and then calculate and print the area of triangle:

# Python Program - Calculate Area of Triangle

print("Enter 'x' for exit.");
side1 = input("Enter length of first side: ");
if side1 == 'x':
    exit();
else:
    side2 = input("Enter length of second side: ");
    side3 = input("Enter length of third side: ");
    a = float(side1);
    b = float(side2);
    c = float(side3);
    s = (a + b + c)/2;
    area = (s*(s-a)*(s-b)*(s-c)) ** 0.5;
    print("\nArea of Triangle = %0.2f" %area);

Here is the sample run of the above Python program shows how to calculate area of a triangle:

calculate area of triangle python

Now enter length of all the three sides of triangle, that is length, breadth, and height say 10, 11, and 12 respectively and then press enter to see the area of this triangle as shown in the following sample output:

area of triangle python

Below is the same program with run on python shell:

python calculate area of triangle

Program Video with Compile and Run


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test