Python Program to calculate the Area of a Triangle

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

  1. Start
  2. Take input: Prompt the user to enter the base of the triangle.
  3. Take input: Prompt the user to enter the height of the triangle.
  4. Calculate the area: Use the formula Area = 0.5 * base * height to compute the area of the triangle.
  5. Display the result: Print the calculated area.
  6. 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

  1. Taking input from the user:
    The program uses input() to ask the user for the base and height of the triangle. The inputs are converted to float so that they can handle both whole numbers and decimals.
  2. Calculating the area:
    The area is calculated using the formula Area = 0.5 * base * height. This is the standard formula for finding the area of a triangle.
  3. Displaying the output:
    The print() function is used to display the result in a formatted manner, showing the calculated area of the triangle.

Python

4730

391

Related Articles