C Programming

How to Find the Volume of a Triangular Prism in C

A prism is a 3D object that has uniform cross-sections, flat rectangular side faces, and identical bases. Prisms come in many forms and are named based on the geometry of their base. For instance, a triangular prism has two identical triangular bases, three rectangular lateral faces, 9 edges, and 6 vertices. The amount of space occupied by a triangular prism in all three dimensions is its volume.

Calculating the volume of a triangular prism in mathematics can be a time-consuming process. However, it is possible to simplify this calculation by creating a straightforward C program that takes input from the user and efficiently computes the volume.

How to Find the Volume of the Triangular Prism?

The volume of a triangular prism is the space it occupies or contains. To calculate the volume of a triangular prism, we need to know the dimensions of its base area and length. The volume is obtained by multiplying the base area and length. The unit of measurement for volume is cubic meters (m³).

The formula for calculating the volume of a triangular prism is:

V = B × l

Where:

  • V represents the volume.
  • B represents the base area.
  • l represents the length of the prism.

The following equation is used to calculate a triangular prism’s base area:

B = 1/2 × b × h

Where,

  • B represents the base area.
  • b represents the triangular base.
  • h represents the height of the prism.

Now we understand how to find the volume of the triangular prism in mathematics. Let’s write a C program that finds the volume of the triangular prism.

C Program to Find the Volume of the Triangular Prism

The given C program calculates the volume of the triangular prism based on the values of base, height, and length entered by the user.

#include <stdio.h>

int main()

{

    float base, height, length, Base_Area;                            
    float volume = 0;                                    
    printf("\nEnter Base: ");                              
    scanf("%f", &base);                              
    printf("\nEnter Height: ");                            
    scanf("%f", &height);    
    printf("\nEnter Length: ");                            
    scanf("%f", &length);
    //Calculate Base area of the triangular prism
    Base_Area = ((float)1/(float)2)*base*height;
    //Calculate volume of triangular prism
    volume = Base_Area*length;        
    printf("Volume of a triangular prism is: %.2f m³", volume);
    return 0;


}

Conclusion

A triangular prism looks like a polyhedron and has 2 triangular identical bases along with 3 rectangular side faces. The volume of the triangular prism refers to the contained space or region. The volume of the triangular prism can be calculated mathematically, although it can take a long time. We created a simple C program that accepts input from the user and quickly calculates the volume to make this calculation efficient.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.