PHP Program to find the area of triangle


October 20, 2023, Learn eTutorial
1558

Area of Triangle

There are mathematical formulas to find the area of the triangle. To find the area of a triangle first we have to get the base value and the height value of the triangle and use the formula (base * height) / 2. For example, if the base of the triangle is 7 and the height of the triangle is 9 then the area of the triangle will be (7 * 9) / 2 which will be 31.5.

find area of triangle

How to find the area of a triangle using PHP

In PHP there are many ways to find the area of the triangle. In this program, we first have to read the base value and the height value from the user and assign it to variables b and h, and to find the area we have to use the formula (base * height) / 2. 

ALGORITHM

Step 1: Read the base value into the variable b

Step 2: Read the height value into the variable h

Step 3: Assign the computed value of '(b * h) / 2' to the variable a

Step 4: Print the value of the variable a as the area of the triangle

PHP Source Code

                                          <?php
$b = readline("Enter the base value: ");
$h = readline("Enter the height value: ");
$a = ($b * $h) / 2;
echo "Area with base $b and height $h = $a";
?>
                                      

OUTPUT

Enter the base value: 5
Enter the height value: 7
Area with base 5 and height 7 = 17.5