Find Hypotenuse of a Number In TypeScript

The longest side of a right-angled triangle and the side that faces away from the right angle is known as the hypotenuse. The Pythagorean theorem states that the hypotenuse's square equals the sum of the squares of the other two sides. The formula is c² = a² + b², where c is the hypotenuse and a and b are the triangle's two sides.

In TypeScript, we can create functions to calculate the hypotenuse using the Pythagorean theorem. The function accepts the lengths of the two shorter sides as parameters and returns the hypotenuse length. This only works for right triangles where one angle is exactly 90 degrees.

Syntax

The basic function can be defined as follows:

function hypotenuse(a: number, b: number): number {
   return Math.sqrt(a * a + b * b);
}

This function takes two arguments, a and b, representing the lengths of the two shorter sides. It calculates the hypotenuse by adding the squares of a and b, then returns the square root of that sum.

Method 1: Using Basic Math Operations

This example demonstrates finding the hypotenuse using basic multiplication and the Math.sqrt() method:

function hypotenuse(a: number, b: number): number {
  return Math.sqrt(a * a + b * b);
}

let side1: number = 3;
let side2: number = 4;

let hypotenuseValue: number = hypotenuse(side1, side2);
console.log(`The hypotenuse of the triangle with sides ${side1} and ${side2} is ${hypotenuseValue}.`);
The hypotenuse of the triangle with sides 3 and 4 is 5.

Method 2: Using Math.pow() Function

This approach uses Math.pow() to square the values explicitly:

function findHypotenuse(a: number, b: number): number {
   return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}

let sideA: number = 5;
let sideB: number = 12;

let hypotenuseResult: number = findHypotenuse(sideA, sideB);
console.log(`The hypotenuse of the triangle with sides ${sideA} and ${sideB} is ${hypotenuseResult}.`);
The hypotenuse of the triangle with sides 5 and 12 is 13.

Comparison

Method Performance Readability
Basic Math (a * a) Faster Simple and clear
Math.pow(a, 2) Slightly slower More explicit

Complete Example with Input Validation

function calculateHypotenuse(a: number, b: number): number {
   if (a <= 0 || b <= 0) {
       throw new Error("Sides must be positive numbers");
   }
   return Math.sqrt(a * a + b * b);
}

// Test with different values
console.log("Hypotenuse of 3,4:", calculateHypotenuse(3, 4));
console.log("Hypotenuse of 5,12:", calculateHypotenuse(5, 12));
console.log("Hypotenuse of 8,15:", calculateHypotenuse(8, 15));
Hypotenuse of 3,4: 5
Hypotenuse of 5,12: 13
Hypotenuse of 8,15: 17

Conclusion

TypeScript provides efficient ways to calculate the hypotenuse using the Pythagorean theorem. Both methods work well, but basic multiplication (a * a) is generally preferred for better performance in mathematical calculations.

Updated on: 2026-03-15T23:19:00+05:30

426 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements