Python Program to Find the Area of a Triangle

Triangles are one of the most fundamental shapes in geometry and often appear in various mathematical problems. The area of a triangle can be calculated using its base and height. In this blog, we will learn how to write a Python program to find the area of a triangle.

Before we proceed to the implementation of the program, let’s briefly understand the formula for calculating the area of a triangle. The formula for the area of a triangle is:

Area = (base * height) / 2

where base and height are the two sides of a triangle. Using this formula, we can easily calculate the area of any triangle, given its base and height.

Now, let’s move to the implementation of the program.

Python Program to Find the Area of a Triangle:

We will use the following steps to write a Python program to find the area of a triangle:

Step 1: Take the input from the user for the base and height of the triangle.

Step 2: Calculate the area of the triangle using the formula (base * height) / 2.

Step 3: Print the area of the triangle.

Here’s the Python code to find the area of a triangle:

# Taking input from the user for base and height of the triangle
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))

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

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

In the above code, we first take input from the user for the base and height of the triangle using the input() function. We then calculate the area of the triangle using the formula (base * height) / 2. Finally, we print the area of the triangle using the print() function.

Let’s understand the code step-by-step:

  • In the first line of the code, we use the input() function to take input from the user for the base of the triangle. We convert the input value to a float using the float() function and store it in the variable base.
  • In the second line of the code, we use the input() function again to take input from the user for the height of the triangle. We convert the input value to a float using the float() function and store it in the variable height.
  • In the third line of the code, we calculate the area of the triangle using the formula (base * height) / 2 and store the value in the variable area.
  • In the last line of the code, we use the print() function to print the area of the triangle on the console.

Let’s run the above code with some sample inputs to see the output:

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

Follow us on Twitter: Hacktube5

Follow us on Youtube: Hacktube5

Leave a Reply