How to compute the hypotenuse h of the triangle in TypeScript

1 Answer

0 votes
function calculateHypotenuse(a: number, b: number): void {
  // Method 1: Using Math.pow and Math.sqrt
  const h1: number = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
  console.log("The hypotenuse (h) is:", h1.toFixed(6));

  // Method 2: Using direct multiplication
  const h2: number = Math.sqrt(a * a + b * b);
  console.log("The hypotenuse (h) is:", h2.toFixed(6));

  // Method 3: Using Math.hypot
  const h3: number = Math.hypot(a, b);
  console.log("The hypotenuse (h) is:", h3.toFixed(6));
}

calculateHypotenuse(7, 5);

  
  
  
/*
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
...