Here is a Python program that calculates the area of a triangle given the base and height:

Created with Sketch.

Here is a Python program that calculates the area of a triangle given the base and height:

base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))

# Calculate the area of the triangle
area = (base * height) / 2

# Print the area of the triangle
print("The area of the triangle is", area)

The program uses the formula for the area of a triangle (base * height / 2) to calculate the area. It prompts the user to enter the base and height of the triangle using the input() function and stores the values in variables “base” and “height”. It then calculates the area by multiplying the base and height and dividing by 2. Finally it prints the area of the triangle using the print() function.

Alternatively, you can also find the area of a triangle using Heron’s formula if you know the length of all three sides of the triangle.

import math

a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
c = float(input("Enter the length of side c: "))

# Calculate the semi-perimeter
s = (a + b + c) / 2

# Calculate the area using Heron's formula
area = math.sqrt(s * (s - a) * (s - b) * (s - c))

# Print the area of the triangle
print("The area of the triangle is", area)

Here, it uses Heron’s formula to calculate the area which is Area = sqrt(s(s-a)(s-b)(s-c)) where s = (a+b+c)/2

Please note that the math library should be imported to use sqrt() function.

Leave a Reply

Your email address will not be published. Required fields are marked *