Python Program to Calculate the Area of a Triangle

Here’s an example Python code to calculate the area of a triangle:

# Take input from the user
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 = 0.5 * base * height

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

In this code, we use the input() function to prompt the user to enter the base and height of the triangle. We convert the user input to a float using the float() function and assign them to the variables base and height.

Next, we calculate the area of the triangle using the formula 0.5 * base * height and assign the result to the variable area.

Finally, we use the print() function to output the area of the triangle.

When you run this code, it should prompt you to enter the base and height of the triangle. Once you enter those values and press enter, it should output the area of the triangle. For example:

Enter the base of the triangle: 5
Enter the height of the triangle: 8
The area of the triangle is 20.0

Note that you can replace 5 and 8 with any other values you want to calculate the area of a triangle with a different base and height.

Related Examples

1. Python Program to Print Hello world!

2. Calculate the multiplication and sum of two numbers

3. Python Program to Find the Square Root

4. Python Program to Solve Quadratic Equation

5. Python Program to Swap Two Variables

6. Python Program to Generate a Random Number

7. Return the count of a given substring from a string

8. Print the following pattern

9. Python Program to check if the given number is a palindrome number

10. Python Program to print multiplication table form 1 to 10

11. Print downward Half-Pyramid Pattern with Star (asterisk)

12. Python Program to Convert Kilometers to Miles

13. Python Program to Check if a Number is Positive, Negative or 0

14. Python Program to Find the Sum of Natural Numbers

15. Print the sum of the current number and the previous number in Python

Leave a Reply

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

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial