Emmanuel Akinfulubi

JavaScriptMath

Volume and surface area of a cylinder and cone in JavaScript

Intermediate · 4 min read

A can of tomatoes is a cylinder: two equal circles joined by a curved side. An ice cream cone, a traffic cone and a funnel are cones: one circle narrowing to a point. To know how much a water tank holds or how much metal or paper you need to make one, you need their volume and surface area.

In this tutorial you will write those formulas for radius 3 and height 10, see that three cones exactly fill the matching cylinder, and learn why where you put the "divide by 3" changes the last digit. You need to know the area of a circle (π × r²), arrow functions and Math.hypot.

The cylinder

  • Volume: the base circle's area times the height, π × r² × h.
  • Lateral surface: the curved side. Cut it down the middle and unroll it, and you get a rectangle. One edge is the circle's circumference, 2πr. The other is the height. So its area is 2 × π × r × h.
  • Total surface: the curved side plus the two circles on the ends. That is 2πrh + 2πr², which factors to 2 × π × r × (r + h).

The cone

  • Volume: exactly a third of the cylinder with the same base and height, π × r² × h ÷ 3. Fill a cone with water and pour it into the matching cylinder three times, and the cylinder is full.
  • Slant height (l): the distance from the tip down the side to the edge of the base. The height, the radius and the slant height make a right-angled triangle, so l is Math.hypot(r, h).
  • Surface: the base circle plus the sloping side, π × r × (r + l).

Here is the exercise. Have a go yourself first if you like, then read on and we will build it together.

77 A cylinder and a cone

fair

For radius 3 and height 10, give the cylinder's volume, its lateral surface and its total surface. Then do the cone: its volume, its slant height and its surface. Show that three cones fill the cylinder, and that where you put the / 3 changes the answer.

Stuck? Three hints, smallest first
  1. 1. A cylinder is pi * r ** 2 * h; a cone on the same base and height is a third of it.
  2. 2. The cone's slant height is the hypotenuse of r and h.
  3. 3. Dividing at the end and multiplying by 1 / 3 at the start are not the same floating-point operation.

Step 1: the cylinder

Step 1

const cylinderVolume = (r, h) => Math.PI * r ** 2 * h;
const cylinderLateral = (r, h) => 2 * Math.PI * r * h;
const cylinderSurface = (r, h) => 2 * Math.PI * r * (r + h);
const r = 3;
const h = 10;
console.log(cylinderVolume(r, h));  // 282.7433388230814
console.log(cylinderLateral(r, h)); // 188.49555921538757
console.log(cylinderSurface(r, h)); // 245.04422698000386

The factored form 2πr(r + h) is shorter than writing both parts out, and it does fewer multiplications. You can check it gives the same answer as the two parts added together:

JavaScript

console.log(cylinderSurface(r, h) === cylinderLateral(r, h) + 2 * Math.PI * r ** 2); // true

Step 2: the cone's volume

Step 2

const coneVolume = (r, h) => (Math.PI * r ** 2 * h) / 3;      // divide last
console.log(coneVolume(r, h));                           // 94.2477796076938
console.log(3 * coneVolume(r, h) === cylinderVolume(r, h)); // true

Three cones make the cylinder, exactly, down to the last digit. Dividing by 3 and then multiplying by 3 undoes itself here.

Step 3: put the third first

Most textbooks write the cone formula as ⅓ × π × r² × h. It is the same in maths. Try it in JavaScript:

Step 3

const coneVolumeThirdFirst = (r, h) => (1 / 3) * Math.PI * r ** 2 * h;
console.log(coneVolumeThirdFirst(r, h));                    // 94.24777960769379
console.log(coneVolumeThirdFirst(r, h) === coneVolume(r, h)); // false

It comes out one tiny step lower. The reason: one third can't be stored exactly in binary, just as it can't be written exactly in decimal (0.333…). So 1 / 3 is already rounded before any multiplying starts, and that small error is carried through every step. Dividing at the end avoids that extra rounding: it rounds once, at the division.

The rule of thumb: do the inexact step, like dividing by 3, as late as you can.

Step 4: the slant height and the cone's surface

Step 4

const slantHeight = (r, h) => Math.hypot(r, h);
const coneSurface = (r, h) => Math.PI * r * (r + slantHeight(r, h));
console.log(slantHeight(r, h));                     // 10.44030650891055
console.log(slantHeight(r, h) === Math.sqrt(109));  // true
console.log(coneSurface(r, h));                     // 126.6719045711654

3² + 10² = 109, so the slant height is √109, a little more than the height of 10. The cone's surface uses the slant height, not the height. Using h by mistake gives a smaller, wrong answer.

Step 5: double the radius

Step 5

console.log(cylinderVolume(6, h) === 4 * cylinderVolume(3, h)); // true

The radius is squared in the volume formula, so doubling it makes the volume four times bigger. Doubling the height only doubles the volume, because the height isn't squared.

The full solution

The answer panel has the cylinder volume and surface functions, both versions of the cone volume, slantHeight and coneSurface, and prints the cylinder's volume and surfaces, the cone's volume both ways, three cones, the slant height, the cone's surface and the doubled radius. Press Run to see the output.

The answer

const cylinderVolume = (r, h) => Math.PI * r ** 2 * h;
const cylinderLateral = (r, h) => 2 * Math.PI * r * h;
const cylinderSurface = (r, h) => 2 * Math.PI * r * (r + h);

const coneVolume = (r, h) => (Math.PI * r ** 2 * h) / 3;      // divide last
const coneVolumeThirdFirst = (r, h) => (1 / 3) * Math.PI * r ** 2 * h;
const slantHeight = (r, h) => Math.hypot(r, h);
const coneSurface = (r, h) => Math.PI * r * (r + slantHeight(r, h));

const r = 3;
const h = 10;

console.log('cylinder volume :', cylinderVolume(r, h));
console.log('lateral, total  :', cylinderLateral(r, h), cylinderSurface(r, h));
console.log('cone volume     :', coneVolume(r, h));
console.log('with 1/3 first  :', coneVolumeThirdFirst(r, h), '<- one bit lower');
console.log('three cones     :', 3 * coneVolume(r, h));
console.log('slant height    :', slantHeight(r, h), 'which is sqrt(109)');
console.log('cone surface    :', coneSurface(r, h));
console.log('radius doubled  :', cylinderVolume(6, h), 'over', cylinderVolume(3, h));

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('the cylinder holds 282.7433388230814 cubic units', cylinderVolume(r, h), 282.7433388230814);
test('its side unrolls to a rectangle of area 188.49555921538757', cylinderLateral(r, h), 188.49555921538757);
test('and the total surface adds two circles, giving 245.04422698000386', [cylinderSurface(r, h), cylinderSurface(r, h) === cylinderLateral(r, h) + 2 * Math.PI * r ** 2], [245.04422698000386, true]);
test('the cone on the same base is a third: 94.2477796076938', coneVolume(r, h), 94.2477796076938);
test('and three of them refill the cylinder exactly', 3 * coneVolume(r, h) === cylinderVolume(r, h), true);
test('but multiplying by 1 / 3 first lands one bit lower', [coneVolumeThirdFirst(r, h), coneVolumeThirdFirst(r, h) === coneVolume(r, h)], [94.24777960769379, false]);
test('the slant height is the hypotenuse of 3 and 10', [slantHeight(r, h), slantHeight(r, h) === Math.sqrt(109)], [10.44030650891055, true]);
test('the cone surface is pi r (r + l), which is 126.6719045711654', coneSurface(r, h), 126.6719045711654);
test('doubling the radius quadruples the volume, exactly here', cylinderVolume(6, h) === 4 * cylinderVolume(3, h), true);

The relationship worth remembering is that a cone is a third of the cylinder on the same base and height — and here 3 * coneVolume(3, 10) === cylinderVolume(3, 10) holds to the last bit, because dividing by 3 and multiplying by 3 undo each other on a double when no rounding was lost in between.

Which is exactly why (1 / 3) * Math.PI * r ** 2 * h is a bit lower: 1 / 3 is not representable, so that version rounds twice — once storing a third, once multiplying — where (Math.PI * r ** 2 * h) / 3 rounds once. Divide at the end. It is the same lesson as the weighted mean in exercise 62: fewer inexact operations, applied later. The surface formulas are worth writing factored, 2 pi r (r + h) and pi r (r + l), both because they are shorter and because they multiply once rather than twice.

Common mistakes

  • Using the diameter as r. A can 8 cm across has a radius of 4. Using 8 makes the volume four times too big.
  • Using the height in the cone's surface. The sloping side uses the slant height, Math.hypot(r, h).
  • Multiplying by 1 / 3 first. It rounds one extra time. Divide by 3 at the end.

Try it yourself

  1. A water tank is a cylinder 1.2 m across and 1.5 m tall. How many liters does it hold? (1 m³ is 1000 liters.)
  2. How much paper do you need for an ice cream cone with radius 3 cm and height 10 cm, with no base?
  3. Write cylinderHeightForVolume(r, v) that returns the height a cylinder of radius r needs to hold volume v.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.