Calculate Volume of Prism in Java



A Prism refers to a three-dimensional solid object which has two identical ends. A prism has two faces, which are:

  • Top and Bottom face.
  • Lateral face

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

Here are the formulas to calculate the volume of the prism (i.e., rectangular and triangular):

//for rectangular prism
Volume = length x width x height

//for triangular prism
Volume = 1/2 x length x width x height

In this article, we will discuss how we can calculate the volume of the Triangular and Rectangular prism in Java.

Input & Output Scenarios:

Following are the scenarios of how to calculate the volume of the prism using mathematical calculation:

Scenario 1:

Suppose we have below details of the Rectangular prism:

Input: length = 4.5, width = 7.5, height = 9.5
Output: Volume = 4.5 x 7.5 x 9.5 = 320.625

Scenario 2:

Suppose we have below details of the Triangular prism:

Input: Length = 12, width = 10, and height = 8
Output: volume = (12 x 10 x 8)/2 = 480.0

Here is a list of different prisms for which we will calculate the volume:

Calculating Volume of Rectangular Prism

A rectangular prism is a solid shape that has rectangular faces. To calculate the volume of a rectangular prism, we use the formula "length x width x height".

Example

In the following example, we calculate the volume of the rectangular prism, which has a length l = 3.5, a width w = 9, and a height h = 12:

public class prismVolume {
   public static void main(String args[]) {
   
      double l = 3.5;
      double w = 9.0;
      double h = 12.0;
      
      System.out.println("The length of prism is: l = " + l);
      System.out.println("The width of prism is: w = " + w);
      System.out.println("The height of prism is: h = " + h);
      
      //formula to calculate the volume of the prism
      double volume = l * w * h;
      
      System.out.println("The volume of the rectangular prism is: volume = " + volume);
   }
}

The above program produces the following output:

The length of prism is: l = 3.5
The width of prism is: w = 9.0
The height of prism is: h = 12.0
The volume of the rectangular prism is: volume = 378.0

Calculating Volume of Triangular Prism

A triangular prism is a solid shape that has triangular faces. To calculate its volume, we use the formula 1/2 x "length x width x height".

Example

The example below defines a function named calculateVolume(length, width, height) which accepts three parameters and and calculates the volume of the triangular prism, which has a length l = 16, width w = 10, and height h = 8:

public class prismVolume {

   //define a method to calculate the volume of the prism
   public static double calculateVolume(double length, double width, double height){
      double volume = (length * width * height)/ 2;
      return volume;
   }
   public static void main(String args[]) {

      double l = 16.0;
      double w = 10.0;
      double h = 8.0;

      System.out.println("The length of prism is: l = " + l);
      System.out.println("The width of prism is: w = " + w);
      System.out.println("The height of prism is: h = " + h);

      //calling method to calculate the volume
      System.out.println("The volume of the triangular prism is: volume = " + calculateVolume(l, w, h));
   }
}

Following is the output of the above program:

The length of prism is: l = 16.0
The width of prism is: w = 10.0
The height of prism is: h = 8.0
The volume of the triangular prism is: volume = 640.0
Updated on: 2025-05-26T19:09:12+05:30

921 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements