Program to calculate the Surface Area of a Triangular Prism


Let us see how to write a program to calculate the Surface Area of a Triangular Prism.

It might seem very basic to calculate the surface area of a triangular prism, but there are various areas where programmers may feel the need for it. Some of the common scenarios are listed below −

  • 3D graphics and animation − While constructing 3D models, animators and game developers may need to compute the surface area of a triangular prism in order to correctly represent it in a virtual world.

  • Engineers and architects may need to determine the surface area of a triangular prism while designing structures such as buildings or bridges.

  • Mathematics and geometry − Programmers working on math or geometry-related projects may need to compute the surface area of a triangular prism as part of their algorithms.

  • Physics simulations − The surface area of a triangular prism may be a crucial metric in some physics simulations, such as those involving fluid dynamics or electromagnetic fields.

Thus, we as programmers might need to calculate the surface area of a triangular prism in some cases, to ensure accuracy and precision.

Explanation

Look at the diagram below of a triangular prism, and observe the image, the edges, and the faces.

The formula for calculating the surface area of a triangular prism is −

Surface area = $\mathrm{(b\:*\:h)\:+\:(L\:*\:(s1\:+\:s2\:+\:s3));}$

Where b= base

h= height

L = Length

s1= side 1 of the triangle face

s2= side 2 of the triangle face

s3= side 3 of the triangle face

Approach

Let’s try to figure out the steps involved in writing the program, in other words, let’s write a step-by-step algorithm.

  • Take the input for the variables.

    Here we need to take input for six variables as we are working with a triangular prism. The variables needed are −

    • base − base of the triangle

    • height − height of the triangle

    • length − length of the prism

    Apart from these we also need the input for all three sides of the triangle −

    • side1 − length of the first side of the triangle

    • side2 − length of the second side of the triangle

    • side3 − length of the third side of the triangle

  • Next, we will calculate the area of the triangular prism using the formula −

    surface_area = (base * height) + (length * (side1 + side2 + side3))

Here, for one triangular face of a prism, the surface area= 0.5 * base * height. So for two triangular faces, total surface area = base * height.

The length * perimeter covers the remaining surface area.

  • Finally, we will print the calculated surface area −

Let’s do some code now.

Example

C++ Program for calculating the Surface Area of a Triangular Prism

#include <iostream>
using namespace std;

int main() {
   double base = 3, height = 6, length = 9, side1 = 3, side2 = 4, side3 = 5;

   // calculate the surface area of the  triangular prism
   double surface_area_of_triangular_faces= base * height;
   double perimeter = side1+side2+side3;
   double surface_area_of_rectangular_faces= length * perimeter; 
   double surface_area_of_triangular_prism = surface_area_of_triangular_faces+ surface_area_of_rectangular_faces ;
   
   // print the result
   cout << "The surface area of the triangular prism is: " << surface_area_of_triangular_prism << endl;
   
   return 0;
}

Output

The surface area of the triangular prism is: 126

Complexities

Time complexity: O(1), As this code performs a fixed number of calculations, regardless of the size of the input.

Space complexity: O(1), As the code uses a fixed number of variables to store input values and results, regardless of the size of the input.

Conclusion

In this article, we have tried to explain the approach to calculate the surface area of a triangular prism with sides of triangular face as input along with the length, base, and height. I hope this article helps you to learn the concept in a better way.

Updated on: 23-Mar-2023

276 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements