Horje
JavaScript program to find volume of cylinder

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).




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Change Button Label in Alert Box using JavaScript ? How to Change Button Label in Alert Box using JavaScript ?
How to work with Structs in JavaScript ? How to work with Structs in JavaScript ?
How to Check if JSON Key Value is Null in JavaScript ? How to Check if JSON Key Value is Null in JavaScript ?
How to Call a JavaScript Function from an onmouseout Event ? How to Call a JavaScript Function from an onmouseout Event ?
Permutations in which n People can Occupy r Seats in a Classroom using JavaScript Permutations in which n People can Occupy r Seats in a Classroom using JavaScript

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13