find the volume of a cylinder in typescript

To find the volume of a cylinder in TypeScript, we need to know its radius and height. We can use the formula:

index.ts
volume = π * radius^2 * height
31 chars
2 lines

where π is a mathematical constant approximately equal to 3.14. Here's an example TypeScript function that calculates the volume of a cylinder:

index.ts
function cylinderVolume(radius: number, height: number): number {
  const pi = Math.PI;
  const volume = pi * radius * radius * height;
  return volume;
}
155 chars
6 lines

In this function, we declare two parameters radius and height as numbers. Math.PI is a built-in constant in TypeScript that approximates the value of π. We then calculate the volume using the formula and return the result.

Here's an example of calling the function:

index.ts
const radius = 5;
const height = 10;
const volume = cylinderVolume(radius, height);
console.log(volume); // Output: 785.3981633974483
134 chars
5 lines

In this example, we declare two variables radius and height and pass them as arguments to the cylinderVolume function. The function calculates the volume and returns the result, which we log to the console.

gistlibby LogSnag