How To Calculate Volume of Prism in Java?


A prism refers to a three−dimensional solid object which has two identical ends. A prism has 2 faces, the first one top and bottom face and second one is lateral faces. Both top and bottom faces are called bases which are identical to each other. All lateral faces are also identical to each other which belong to the class of parallelograms.

When the base of the prism is triangular in shape that prism is called a triangular prism. Similarly, when the base of a prism is rectangular in shape it is called a rectangular prism. Other types of prisms are also available like pentagonal prism, square prism and hexagonal prism etc.

Volume of the prism refers to the space/region acquired by the prism. We can calculate the volume of the prism by using length, width and height of the prism

Formula to calculate volume of triangular prism −

Volume = 1/2 x length x width x height

Formula to calculate volume of rectangular prism −

Volume = length x width x height

In this article we will see how we can find the volume of the triangular and rectangular prism in Java.

To show you some instances

Instance-1

Suppose we have below details of the triangular prism.

Length(l) is 12, width(b) is 10 and height(h) is 8

Then by using volume formula of triangular prism

Volume = 480.0

Hence, the volume of the rectangular prism is 480.0

Instance-2

Suppose we have below details of the rectangular prism.

Length(l) is 4.5, width(b) is 7.5 and height(h) is 9.5 

Then by using volume formula of rectangular prism

Volume = 320.625

Hence, the volume of the rectangular prism is 320.625

Instance-3

Suppose we have below details of the triangular prism

Length(l) is 8, width(b) is 6 and height(h) is 7

Then by using volume formula of triangular prism

Volume = 168.0

Hence, the volume of the rectangular prism is 168.0

Algorithm

  • Step 1 − Get the length, width and height of the prism either by initialization or by user input.

  • Step 2 − Find the volume of the prism by using the volume formula.

  • Step 3 − Print the result.

Multiple Approaches

We have provided the solution in different approaches

  • Find Volume of Rectangular Prism.

  • Find Volume of Triangular Prism.

Let’s see the program along with its output one by one.

Approach-1: Find Area of Rectangular Prism

In this approach, the length, width and height value of the prism will be initialized in the program. Then by using the volume formula of the rectangular prism, find the volume

Example

import java.util.*; public class Main { //main method public static void main(String args[]) { //initialized length of prism double l=3.5; System.out.println("The given length of prism: "+l); //initialized width of prism double b=9; System.out.println("The given width of prism: "+b); //initialized height of prism double h=12; System.out.println("The given height of prism: "+h); //Find volume by using formula double volumeOfPrism= l * b * h; //Print the result System.out.println("Volume of rectangular prism: " +volumeOfPrism); } }

Output

The given length of prism: 3.5
The given width of prism: 9.0
The given height of prism: 12.0
Volume of rectangular prism: 378.0

Approach-2: Find Area of Triangular Prism

In this approach, the length, width and height value of the prism will be initialized in the program. Then by using the volume formula of a triangular prism, find the volume.

Example

public class Main { //main method public static void main(String args[]){ //initialized length of prism double l=16; System.out.println("The given length of prism: "+l); //initialized width of prism double b=10; System.out.println("The given width of prism: "+b); //initialized height of prism double h=8; System.out.println("The given height of prism: "+h); //Find volume by using formula double volumeOfPrism= (l * b * h) / 2; //Print the result System.out.println("Volume of triangular prism: " +volumeOfPrism); } }

Output

The given length of prism: 16.0
The given width of prism: 10.0
The given height of prism: 8.0
Volume of triangular prism: 640.0

In this article, we explored how to find volume prism in Java by using different approaches.

Updated on: 27-Oct-2022

642 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements