Open In App

JavaScript Program to Find Volume of Right Wedge

Last Updated : 14 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A wedge is a three-dimensional object with two triangular faces and one rectangular face.

Examples: 

Input: a = 2, b = 5, e = 5, h = 6 
Output: Volume = 45.0

Input: a = 5, b = 4, e = 4, h = 6 
Output: Volume = 56.0 

Approach: 

WEDGE

Va is the volume of triangular pyramid i.e. Va = (1 / 3) * area of triangle * (e – a) 
Area of triangle = (1 / 2) * b * h 
i.e Va = ( 1 / 3 ) * (( 1 / 2 ) * ( b * h * (e – a))) 
Vb is a volume of triangular prism i.e. Vb = area of cross section * length(side) 
i.e. Vb = (1 / 2) * (b * h * a) 

Total Volume = Va + Vb 
= (1 / 3) * ((1 / 2) * (b * h * ( e – a ))) + (1 / 2) * (b * h * a) 
= (1 / 6) * (b * h * (e – a)) + (1 / 2) * (b * h * a) 
= ((b * h) * (e – a) + 3 * b * h * a) / 6 
= (b * h * e – b * h * a + 3 * b * h * a) / 6 
= (b * h * e + 2 * b * h * a) / 6 
= (b * h / 6) * (2 * a + e) 

Note: Volume of the rectangular right wedge = (b * h / 6) * (2 * a + e) where a and b are the side bases, e is the top edge and h is the height of the rectangular right wedge.

Using Direct Calculation

In this approach, we are using a direct calculation based on the formula for a rectangular right wedge to find its volume without intermediary calculations of separate components.

Example: The below example uses Direct Calculation to find the volume of the wedge.

JavaScript
let a = 2,
    b = 5,
    e = 5,
    h = 6;
let volume = (b * h / 6) * (2 * a + e);
console.log(`Volume = ${volume.toFixed(1)}`);

Output
Volume = 45.0

Time Complexity: O(1)

Space Complexity: O(1)

Using Separate Variables for Triangle and Prism

In this approach, we are using separate variables to calculate the volumes of the triangular pyramid and triangular prism, then adding them to determine the total volume of the rectangular right wedge.

Example: The below example uses Separate Variables for Triangle and Prism to find the volume of the wedge.

JavaScript
let a = 2,
  b = 5,
  e = 5,
  h = 6;
let Va = (1 / 3) * ((1 / 2) * (b * h * (e - a)));
let Vb = (1 / 2) * (b * h * a);
let res = Va + Vb;
console.log(`Volume = ${res.toFixed(1)}`);

Output
Volume = 45.0

Time Complexity: O(1)

Space Complexity: O(1)


Next Article

Similar Reads