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 explains that the Hypotenuse's square equals the sum of the squares of the other two sides. We can be used to determine it using this theorem. The formula representation for this theorem is c2 = a2 + b2, where c means the Hypotenuse and a and b are the triangle's two sides. When the lengths of the other two sides of a triangle are known, the Pythagorean theorem quickly determines the value of the Hypotenuse. First, we have to take the square root of the sum of the squares on the other two sides to obtain the Hypotenuse.

The Pythagorean theorem can be used to compute the Hypotenuse in TypeScript by writing a function that accepts the lengths of the two shorter sides as parameters. As a result, the function returns the Hypotenuse. There is a condition to apply this theorem and find the Hypotenuse. The triangle must be a right triangle for this function to work, necessitating that one of the angles must be a right angle (90 degrees). The Pythagorean theorem cannot be applied to determine the Hypotenuse if the triangle is not a right triangle. We will be describing the function of typescript using two examples.

Syntax

The 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 of the triangle. It then calculates the square of the hypotenuse by adding the squares of a and b and finally returns the square root of that sum.

It's important to note that this function assumes that the triangle is a right triangle, meaning that one of the angles is a right angle (90 degrees). If the triangle is not a right triangle, the Pythagorean theorem cannot be used to find the hypotenuse.

Example

In this example, we will find the hypotenuse of a number in TypeScript. The following steps need to be performed, and the explanations are also given below −

Steps

  • We first define a function called hypotenuse that takes two arguments, a and b, which represent the lengths of the two shorter sides of the triangle. This function uses the Pythagorean theorem to calculate the square of the hypotenuse by adding the squares of a and b and then returns the square root of that sum using the Math.sqrt() method in TypeScript.

  • Then we define two variables, side1, and side2, which are the two shorter sides of the triangle. These values are assigned as 3 and 4, respectively.

  • Then we call the hypotenuse function by passing side1 and side2 as arguments, and the result is stored in a variable hypotenuseValue.

  • Finally, we use the console.log() method to print the result in the console.

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}.`
)

On compiling, it will generate the following JavaScript code −

function hypotenuse(a, b) {
   return Math.sqrt(a * a + b * b);
}
var side1 = 3;
var side2 = 4;
var hypotenuseValue = hypotenuse(side1, side2);
console.log("The hypotenuse of the triangle with sides " + side1 + " and " + side2 + " is " + hypotenuseValue + ".");

Output 

The above code will produce the following output –

The hypotenuse of the triangle with sides 3 and 4 is 5.

Example

In this example, we will find the hypotenuse of a number using the Math.pow and Math.sqrt methods in TypeScript. The following steps need to be performed, and the explanations are also given below −

Steps

  • We have created a function called findHypotenuse that takes two arguments, a and b, which represent the lengths of the two shorter sides of the triangle.

  • Inside the function, we use the Math.pow(base, exponent) method to square the values of a and b and then use the Math.sqrt() method to find the square root of the sum of the squares of a and b. This will give us the hypotenuse of the triangle.

  • Then we define two variables, side A and side B, which are the two shorter sides of the triangle. These values are assigned as 5 and 12, respectively.

  • We then call the findHypotenuse function by passing sides A and B as arguments, and the result is stored in a variable hypotenuse.

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 hypotenuse: number = findHypotenuse(sideA, sideB)
console.log(
   `The hypotenuse of the triangle with sides ${sideA} and ${sideB} is ${hypotenuse}.`
)

On compiling, it will generate the following JavaScript code −

function findHypotenuse(a, b) {
   return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
var sideA = 5;
var sideB = 12;

var hypotenuse = findHypotenuse(sideA, sideB);
console.log("The hypotenuse of the triangle with sides " + sideA + " and " + sideB + " is " + hypotenuse + ".");

Output 

The above code will produce the following output –

The hypotenuse of the triangle with sides 5 and 12 is 13.

Using TypeScript, we can even perform more mathematical calculations efficiently. Finding hypotenuse is one example of it. Also, the results are fast and accurate.

Updated on: 21-Feb-2023

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements