Write a Python program that calculates the area of a triangle when the base and height are provided by the user.
Input-Output Examples
plaintext
Example 1:
Input:
Enter the base of the triangle: 10
Enter the height of the triangle: 5
Output: The area of the triangle is: 25.0
Example 2:
Input:
Enter the base of the triangle: 8
Enter the height of the triangle: 6
Output: The area of the triangle is: 24.0
Algorithm to calculate the Area of a Triangle
- Start
- Take input: Prompt the user to enter the base of the triangle.
- Take input: Prompt the user to enter the height of the triangle.
- Calculate the area: Use the formula
Area = 0.5 * base * height
to compute the area of the triangle. - Display the result: Print the calculated area.
- End
Python Program to calculate the Area of a Triangle
python
# Python program to calculate the area of a triangle
# Take input from the user for base and height
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
# Calculate the area using the formula Area = 0.5 * base * height
area = 0.5 * base * height
# Display the result
print(f"The area of the triangle is: {area}")
Code Explanation
- Taking input from the user:
The program usesinput()
to ask the user for the base and height of the triangle. The inputs are converted tofloat
so that they can handle both whole numbers and decimals. - Calculating the area:
The area is calculated using the formulaArea = 0.5 * base * height
. This is the standard formula for finding the area of a triangle. - Displaying the output:
Theprint()
function is used to display the result in a formatted manner, showing the calculated area of the triangle.