Skip to content

Commit c227bf3

Browse files
Added surface area calculation for pyramid
1 parent 410f087 commit c227bf3

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

  • src/main/java/com/thealgorithms/maths

‎src/main/java/com/thealgorithms/maths/Area.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,21 @@ public static double surfaceAreaCone(final double radius, final double height) {
232232
}
233233
return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
234234
}
235+
* Calculate the surface area of a pyramid with a square base.
236+
*
237+
* @param sideLength side length of the square base
238+
* @param slantHeight slant height of the pyramid
239+
* @return surface area of the given pyramid
240+
*/
241+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
242+
if (sideLength <= 0) {
243+
throw new IllegalArgumentException("Must be a positive sideLength");
244+
}
245+
if (slantHeight <= 0) {
246+
throw new IllegalArgumentException("Must be a positive slantHeight");
247+
}
248+
double baseArea = sideLength * sideLength;
249+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
250+
return baseArea + lateralSurfaceArea;
251+
}
235252
}

0 commit comments

Comments
 (0)