0
0
PythonProgramBeginner · 2 min read

Python Program to Calculate Area of Triangle

You can calculate the area of a triangle in Python using the formula area = 0.5 * base * height. For example, area = 0.5 * 10 * 5 calculates the area when base is 10 and height is 5.
📋

Examples

Inputbase = 10, height = 5
OutputArea of the triangle is 25.0
Inputbase = 3, height = 4
OutputArea of the triangle is 6.0
Inputbase = 0, height = 5
OutputArea of the triangle is 0.0
🧠

How to Think About It

To find the area of a triangle, you need to know its base length and height. The area is half the product of the base and height. So, multiply base and height, then divide by 2 to get the area.
📐

Algorithm

1
Get the base length from the user
2
Get the height from the user
3
Calculate area by multiplying base and height, then dividing by 2
4
Display the calculated area
💻

Code

python
base = float(input('Enter the base of the triangle: '))
height = float(input('Enter the height of the triangle: '))
area = 0.5 * base * height
print('Area of the triangle is', area)
Output
Enter the base of the triangle: 10 Enter the height of the triangle: 5 Area of the triangle is 25.0
🔍

Dry Run

Let's trace the example where base = 10 and height = 5 through the code

1

Input base

User enters base = 10

2

Input height

User enters height = 5

3

Calculate area

area = 0.5 * 10 * 5 = 25.0

4

Print result

Output: Area of the triangle is 25.0

StepVariableValue
1base10
2height5
3area25.0
💡

Why This Works

Step 1: Get inputs

We ask the user to enter the base and height, converting them to numbers with float() so we can do math.

Step 2: Calculate area

We use the formula 0.5 * base * height because the area of a triangle is half the product of its base and height.

Step 3: Show output

We print the result so the user can see the area calculated.

🔄

Alternative Approaches

Using Heron's formula
python
a = float(input('Enter side a: '))
b = float(input('Enter side b: '))
c = float(input('Enter side c: '))
s = (a + b + c) / 2
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
print('Area of the triangle is', area)
This method calculates area using all three sides, useful when height is unknown but sides are known.
Using base and height as integers
python
base = int(input('Enter base: '))
height = int(input('Enter height: '))
area = 0.5 * base * height
print(f'Area of the triangle is {area}')
This uses integer inputs and f-string for output, simpler but less flexible for decimal values.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation involves a fixed number of arithmetic operations, so it runs in constant time.

Space Complexity

Only a few variables are used to store inputs and the result, so space usage is constant.

Which Approach is Fastest?

Both base-height formula and Heron's formula run in constant time, but base-height is simpler and faster since it uses fewer operations.

ApproachTimeSpaceBest For
Base and Height FormulaO(1)O(1)When base and height are known
Heron's FormulaO(1)O(1)When all three sides are known
💡
Always convert input to float to handle decimal values for base and height.
⚠️
Forgetting to multiply by 0.5 and just multiplying base and height, which gives double the actual area.