find the surface area of a cylinder in typescript

To find the surface area of a cylinder in Typescript, you can use the following formula:

Surface Area = 2πr^2 + 2πrh

Where r is the radius of the base of the cylinder and h is the height of the cylinder.

Here is the Typescript code to find the surface area of a cylinder:

index.ts
function surfaceAreaOfCylinder(radius: number, height: number): number {
  const baseArea = Math.PI * Math.pow(radius, 2);
  const sideArea = 2 * Math.PI * radius * height;
  const surfaceArea = 2 * baseArea + sideArea;
  return surfaceArea;
}
244 chars
7 lines

In this code, we first calculate the area of the base of the cylinder (baseArea) using the formula πr^2. We then calculate the area of the side of the cylinder (sideArea) using the formula 2πrh. Finally, we add up the area of the two bases and the side to get the total surface area of the cylinder.

You can call this function by passing in the radius and height of the cylinder, like this:

index.ts
const radius = 5;
const height = 10;
const surfaceArea = surfaceAreaOfCylinder(radius, height);
console.log(`The surface area of the cylinder is: ${surfaceArea}`);
164 chars
5 lines

This will output: The surface area of the cylinder is: 471.23889803846896.

gistlibby LogSnag