Python Program to find area of triangle
Program
base = int(input("Enter the base of the triangle:\t"))
height = int(input("Enter the height of the triangle:\t"))
area = 0.5 * base * height
print("The area of triangle is: {0}".format(area))
This program calculates the area of a triangle using the formula:
[
\text{Area} = \frac{1}{2} \times \text{Base} \times \text{Height}
]
base = int(input("Enter the base of the triangle:\t"))
height = int(input("Enter the height of the triangle:\t"))
- User inputs the base and height of the triangle.
input()
function takes user input as a string.int()
converts the input into an integer.
area = 0.5 * base * height
- Uses the formula for the area of a triangle:
[
\frac{1}{2} \times \text{Base} \times \text{Height} ] - Stores the computed area in the variable
area
.
print("The area of triangle is: {0}".format(area))
- Uses
print()
to display the computed area using string formatting.
Example Execution:
Case 1: Base = 10, Height = 5
User Input:
Enter the base of the triangle: 10
Enter the height of the triangle: 5
Calculation:
[
\frac{1}{2} \times 10 \times 5 = \frac{50}{2} = 25
]
Output:
The area of triangle is: 25.0
Case 2: Base = 6, Height = 12
User Input:
Enter the base of the triangle: 6
Enter the height of the triangle: 12
Calculation:
[
\frac{1}{2} \times 6 \times 12 = \frac{72}{2} = 36
]
Output:
The area of triangle is: 36.0
Key Takeaways:
- Uses the standard formula for the area of a triangle.
- Accepts user input for base and height.
- Displays output with string formatting using
format()
.
Output
Enter the base of the triangle: 6
Enter the height of the triangle: 7
The area of triangle is: 21.0