calcular numero de, lados por, igual in php

To calculate the number of sides of a regular polygon in PHP, you can use the following formula:

main.php
$sides = $_POST['num_sides']; //Assuming the number of sides is coming from form input
if ($sides < 3) {
    echo "A polygon should have at least 3 sides";
} else {
    $interior_angle = 180 * ($sides - 2) / $sides;
    $exterior_angle = 360 / $sides;
    echo "A polygon with $sides sides has interior angle of $interior_angle degrees and exterior angle of $exterior_angle degrees";
}
386 chars
9 lines

In the code above, we first get the number of sides from a form input. We then check if the number of sides is greater than or equal to 3 (a polygon should have at least 3 sides).

If the number of sides is valid, we calculate the interior angle and exterior angle of the polygon using the formulas:

  • Interior angle: interior_angle = 180 * (sides - 2) / sides
  • Exterior angle: exterior_angle = 360 / sides

Finally, we output the number of sides, the interior angle, and the exterior angle of the polygon using echo.

Note that this formula only works for regular polygons (polygons with all sides and angles equal). If you want to calculate the number of sides of an irregular polygon, you need to know the angles and lengths of its sides.

gistlibby LogSnag