Home  >  Article  >  Backend Development  >  Program to calculate the surface area of ​​a triangular prism

Program to calculate the surface area of ​​a triangular prism

WBOY
WBOYforward
2023-09-22 17:57:091202browse

Let's see how to write a program to calculate the surface area of ​​a triangular prism.

Calculating the surface area of ​​a triangular prism may seem very basic, but programmers may need it in many fields. Some common scenarios are listed below -

  • 3D Graphics and Animation - When building 3D models, animators and game developers may need to calculate the surface area of ​​a triangular prism in order to properly represent it in the virtual world.

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

  • Mathematics and Geometry - Programmers working on mathematics or geometry related projects may need to calculate the surface area of ​​a triangular prism as part of their algorithm.

  • Physical Simulation - The surface area of ​​a triangular prism can be a key metric in certain physics simulations, such as those involving fluid dynamics or electromagnetic fields.

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

illustrate

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

Program to calculate the surface area of ​​a triangular prism

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

s3=Side 3 of the triangle

method

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

  • Get the input of the variable.

    Here we need to enter six variables because we are using a triangular prism. The required variables are -

    • Base - The base of the triangle

    • Height - Height of the triangle

    • Length - The length of the prism

    In addition, we also need the input of all three sides of the triangle -

    • side1 - The length of the first side of the triangle

    • side2 - The length of the second side of the triangle

    • side3 - The length of the third side of the triangle

  • Next, we will use the formula to calculate the area of ​​the triangular prism -

    Surface area = (Base * Height) (Length * (Side 1 Side 2 Side 3))

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

Program to calculate the surface area of ​​a triangular prism

Length*Perimeter covers the remaining surface area.

Program to calculate the surface area of ​​a triangular prism
  • Finally, we will print the calculated surface area -

Now let's write some code.

Example

C program to calculate 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

Complexity

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

Space Complexity: O(1) because the code uses a fixed number of variables to store input values ​​and results regardless of the size of the input.

in conclusion

In this article, we try to explain the method of calculating the surface area of ​​a triangular prism using the sides of the triangular faces as input along with the length, base and height. I hope this article helps you learn this concept better.

The above is the detailed content of Program to calculate the surface area of ​​a triangular prism. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete