Open In App

JavaScript program to find volume of cylinder

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

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

Example:

V = πr^2h
where,
Ï€ is constant, value 3.141592653589793
h is height of cylinder
r is radius of cylinder

Approach

  • The code defines a function cylinderVolume that takes two parameters “radius” and “height”.
  • Inside the function, it calculates the volume of a cylinder using the formula “V = Ï€r^2h”, where “r” is the radius and “h” is the height.
  • The Math. pow() function is used to calculate the square of the radius.
  • It then assigns the result to the variable “volume” and returns it.
  • Finally, the code calculates the volume of a cylinder with a given radius and height and logs the result to the console.

Example: JavaScript program to find the volume of a cylinder.

JavaScript
// Using formula

function cylinderVolume(radius, height) {

    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 = cylinderVolume(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