Python Program to Find Area of Triangle
You can find the area of a triangle in Python by using the formula
area = 0.5 * base * height, where base and height are the triangle's base and height values.Examples
Inputbase = 4, height = 3
OutputArea of the triangle is 6.0
Inputbase = 10, height = 5
OutputArea of the triangle is 25.0
Inputbase = 0, height = 7
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.
Algorithm
1
Get the base length from the user2
Get the height from the user3
Calculate area by multiplying base and height, then dividing by 24
Display the calculated areaCode
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: 4
Enter the height of the triangle: 3
Area of the triangle is 6.0
Dry Run
Let's trace the example where base = 4 and height = 3 through the code
1
Input base
User enters 4, so base = 4.0
2
Input height
User enters 3, so height = 3.0
3
Calculate area
area = 0.5 * 4.0 * 3.0 = 6.0
4
Print result
Output: Area of the triangle is 6.0
| Step | Variable | Value |
|---|---|---|
| 1 | base | 4.0 |
| 2 | height | 3.0 |
| 3 | area | 6.0 |
Why This Works
Step 1: Getting inputs
We use input() to get base and height from the user and convert them to float numbers for calculation.
Step 2: Calculating area
The formula 0.5 * base * height calculates the triangle's area by multiplying base and height, then taking half.
Step 3: Displaying output
We print the result with a clear message so the user understands the output.
Alternative Approaches
Using Heron's formula with three sides
python
import math side1 = float(input('Enter side 1: ')) side2 = float(input('Enter side 2: ')) side3 = float(input('Enter side 3: ')) s = (side1 + side2 + side3) / 2 area = math.sqrt(s * (s - side1) * (s - side2) * (s - side3)) print('Area of the triangle is', area)
This method calculates area when you know all three sides, but it is more complex than base-height method.
Using a function to reuse code
python
def triangle_area(base, height): return 0.5 * base * height b = float(input('Base: ')) h = float(input('Height: ')) print('Area of the triangle is', triangle_area(b, h))
This approach organizes code better and allows easy reuse for multiple calculations.
Complexity: O(1) time, O(1) space
Time Complexity
The calculation uses a fixed number of arithmetic operations, so it runs in constant time.
Space Complexity
Only a few variables are used, so space usage is constant.
Which Approach is Fastest?
The base-height formula is simplest and fastest; Heron's formula is slower due to square root and more inputs.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Base and Height Formula | O(1) | O(1) | Simple known base and height |
| Heron's Formula | O(1) | O(1) | Known three sides, no height |
| Function Wrapper | O(1) | O(1) | Reusable code for multiple calculations |
Always convert input to float to handle decimal values for base and height.
Forgetting to convert input strings to numbers causes errors or wrong calculations.