JavaScript Program to find Volume & Surface Area of Cuboid
A Cuboid is a 3-dimensional box-like figure represented in the 3-dimensional plane. Cuboid has 6 rectangle-shaped faces that have length, width, and height of different dimensions. We are going to learn how can we calculate the Volume and Surface Area of a Cuboid using JavaScript.
Formula used:
Surface area of Cuboid = 2 * ((l * h) + (l * b) + (h * b))
Volume of cuboid = l * b * h
// l is length
// b is breadth
// h is height
Example:
Input: l=10, b=30, h=20
Output: Surface area = 2200, Volume = 6000
Approach
- Two functions named surfaceAreaCuboid, and take volumeCuboid are defined in JavaScript, which take length, breadth, and height as three parameters.
- surfaceAreaCuboid function proceeds to calculate the Surface Area of the Cuboid using the formula 2 * ((l * h) + (l * b) + (h * b))
- volumeCuboid function proceeds to calculate the Surface Area of the Cuboid using the formula 2 * ((l * h) + (l * b) + (h * b)) Where l is the length b is the breadth and h is the height of the cuboid.
Example: Below is the function to find the Surface area and Volume of the Cuboid.
// Javascript program to find
// Volumne & Surface Area of Cuboid
function surfaceAreaCuboid(l, b, h) {
return (2 * ((l * h) + (l * b) + (h * b)));
}
function volumeCuboid(l, b, h) {
return (l * b * h);
}
// Driver function
let l = 10;
let b = 30;
let h = 20;
let output1 = surfaceAreaCuboid(l, b, h);
let output2 = volumeCuboid(l, b, h);
console.log(" Surface Area of Cuboid is", output1,
"and the Volume of Cuboid is", output2)
Output
Surface Area of Cuboid is 2200 and the Volume of Cuboid is 6000
Time complexity: O(1), as the execution time is constant and does not grow with the size of the input.
Auxiliary Space complexity: O(1) as it uses a constant amount of memory regardless of the input size.