How to compute the hypotenuse h of the triangle in PHP

1 Answer

0 votes
$a = 7;
$b = 5;

// Method 1: Using pow() and sqrt()
$h1 = sqrt(pow($a, 2) + pow($b, 2));
echo "The hypotenuse (h) is: " . number_format($h1, 6) . PHP_EOL;

// Method 2: Using direct multiplication
$h2 = sqrt($a * $a + $b * $b);
echo "The hypotenuse (h) is: " . number_format($h2, 6) . PHP_EOL;

// Method 3: Using hypot()
$h3 = hypot($a, $b);
echo "The hypotenuse (h) is: " . number_format($h3, 6) . PHP_EOL;



/*
run:

The hypotenuse (h) is: 8.602325
The hypotenuse (h) is: 8.602325
The hypotenuse (h) is: 8.602325

*/

 



answered Jun 29, 2025 by avibootz

Related questions

...