Open In App

JavaScript Program to Find the Volume of Cylinder

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We are going to see JavaScript Program to Find the Volume of Cylinder. We use direct formula to calculate volume of cylinder. A cylinder is a three-dimensional geometric shape that consists of two parallel circular bases connected by a curved surface.

Formula used

V = πr2h

where:

  • V is volume of cylinder
  • Ï€ is mathematical constant whose value is equal to 3.14
  • r is radius of cylinder
  • h is height of cylinder

Approach

We will create a function and pass radius and height as parameter. Calculate the volume of the cylinder using the formula. Return the calculated volume

Example: This example shows the finding the volume of cylinder using direct formula.

JavaScript
// program to calculate volume of cylinder

function VolumeOfCylinder(radius, height) {

    // volume of the cylinder 
    // using the formula V = πr^2h

    let volume = Math.PI *
     Math.pow(radius, 2) * height;
    return volume;
}

// Radius of the cylinder
let radius = 5;

// Height of the cylinder 
let height = 10;

let volume = VolumeOfCylinder(radius, height);
console.log("Volume of the cylinder is :", volume);

Output
Volume of the cylinder is : 785.3981633974483

Time Complexity: O(1)

Space Complexity: O(1)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads