Write Java Program to find Volume and Surface Area of Cuboid with example. Before we step into the Java Program to find Volume and Surface Area of Cuboid, Let see the definitions and formulas behind Area of Top & Bottom Surfaces, Surface area of a Cuboid, Lateral Surface Area and Volume of a Cuboid

## Cuboid

Cuboid is a 3D object made up of 6 Rectangles. All the opposite faces (i.e Top and Bottom) are equal.

### Surface Area of a Cuboid

The Total Surface Area of a Cuboid is the sum of all the 6 rectangles areas present in the Cuboid. If we know the length, width and height of the Cuboid then we can calculate the Total Surface Area using the formula:

- Area of Top & Bottom Surfaces = lw + lw = 2lw
- Area of Front & Back Surfaces = lh + lh = 2lh
- Area of both sides = wh + wh = 2wh

The Total Surface Area of a Cuboid is the sum of all the 6 faces. So, we have to add all these area to calculate the final Surface Area

- Total Surface Area of a Cuboid = 2lw + 2lh + 2wh
- It is equal: Total Surface Area = 2 (lw + lh +wh)

### Volume of a Cuboid

The amount of space inside the Cuboid is called as Volume. If we know the length, width and height of the Cuboid then we can calculate the Volume using the formula:

- Volume of a Cuboid = Length * Breadth * Height
- Volume of a Cuboid = lbh
- The Lateral Surface Area of a Cuboid = 2h (l + w)

## Java Program to find Volume and Surface Area of Cuboid

This Java program allows user to enter the length, width and height of a Cuboid. Using these values, program will calculate the Volume, Surface Area and Lateral Surface Area of Cuboid as per the formulas.

**JAVA CODE**

// Java Program to find Volume and Surface Area of Cuboid package SurfaceAreaPrograms; import java.util.Scanner; public class VolumeOfCuboid { private static Scanner sc; public static void main(String[] args) { // LSA = Lateral Surface Area of a Cube, SA = Surface Area float length, width, height, SA, Volume, LSA; sc = new Scanner(System.in); System.out.println("\n Please Enter the Length of a Cuboid : "); length = sc.nextFloat(); System.out.println("\n Please Enter the Width of a Cuboid : "); width = sc.nextFloat(); System.out.println("\n Please Enter the Height of a Cuboid : "); height = sc.nextFloat(); SA = 2 * (length * width + length * height + width * height); Volume = length * width * height; LSA = 2 * height * (length + width); System.out.format("\n The Surface area of a Cuboid = %.2f", SA); System.out.format("\n The Volume of a Cuboid = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cuboid = %.2f", LSA); } }

**OUTPUT**

**ANALYSIS**

Following statement will allow the User to enter the length, width and height of a cuboid and then we are assigning the user entered values to already declared variable called length, width and height.

System.out.println("\n Please Enter the Length of a Cuboid : "); length = sc.nextFloat(); System.out.println("\n Please Enter the Width of a Cuboid : "); width = sc.nextFloat(); System.out.println("\n Please Enter the Height of a Cuboid : "); height = sc.nextFloat();

Next, we are using the Mathematical Formula to calculate the surface area of a Cuboid

SA = 2 * (length * width + length * height + width * height);

In the next line, We are calculating the volume of a Cuboid

Volume = length * width * height;

In the next line, We are calculating the Lateral Surface Area of a Cuboid

LSA = 2 * height * (length + width);

Following System.out.format statements will help us to print the Volume of a Cuboid, Lateral Surface Area of a Cuboid and Surface Area of a Cuboid

System.out.format("\n The Surface area of a Cuboid = %.2f", SA); System.out.format("\n The Volume of a Cuboid = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cuboid = %.2f", LSA);

From the above Java Program to find Volume and Surface Area of Cuboid screenshot you can observe that, We have entered the Values Length = 10, Width = 6 and Height = 8

The **Volume of a Cuboid** for the specified Measures are:

Volume of a Cuboid = lbh = l * w * h

Volume of a Cuboid = length * width * height

Volume of a Cuboid = 10 * 6 * 8

Volume of a Cuboid = 480

The Volume of a Cuboid is 480

The **Total Surface Area of a Cuboid** for the specified Measures are:

Total Surface Area of a Cuboid = 2lw + 2lh + 2wh ==> 2 (lw + lh +wh)

Total Surface Area of a Cuboid = 2* (length * width + length * height + width * height)

Total Surface Area of a Cuboid = 2 * ( (10 * 6) + (10 * 8) + (6 * 8) )

Total Surface Area of a Cuboid = 2 * (60 + 80 + 48) ==> 2 * (188) ==> 376

The Total Surface Area of a Cuboid is 376

The **Lateral Surface Area of a Cuboid** for the specified Measures are:

Lateral Surface Area of a Cuboid = 2lh + 2wh ==> 2h (l + w)

Lateral Surface Area of a Cuboid = 2 * height * (length + width)

Lateral Surface Area of a Cuboid = 2 * 8 * (10 + 6)

Lateral Surface Area of a Cuboid = 2 * 8 * (16 ) ==> 256

The Lateral Surface Area of a Cuboid is 156

## Java Program to find Volume and Surface Area of Cuboid using Functions

This Java program allows user to enter the length, width and height of a Cuboid. Using these values, it will calculate the Volume, Surface Area and Lateral Surface Area of Cuboid as per the formulas. In this example, we are going to use the logic that we specified in first example but we will separated the logic and place it in a method.

package SurfaceAreaPrograms; import java.util.Scanner; public class VolumeOfCubeUsingMethods { private static Scanner sc; public static void main(String[] args) { // LSA = Lateral Surface Area of a Cube, SA = Surface Area float length; sc = new Scanner(System.in); System.out.println("\n Please Enter the Length of any side of a Cube : "); length = sc.nextFloat(); VolumeOfCube (length); } public static void VolumeOfCube (float length) { float SA,Volume, LSA; SA = 6 * (length * length); Volume = length * length * length; LSA = 4 * (length * length); System.out.format("\n The Surface area of a Cube = %.2f", SA); System.out.format("\n The Volume of a Cube = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cube = %.2f", LSA); } }

**OUTPUT**

## Java Program to find Volume and Surface Area of Cuboid using Oops

In this Java program, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a methods.

package SurfaceAreaPrograms; public class VolumeOfaCuboid { float SA, Volume, LSA; public float VolumeOfCuboid (float length, float width, float height) { Volume = length * width * height; return Volume; } public float SurfaceAreaOfCuboid (float length, float width, float height) { SA = 2 * (length * width + length * height + width * height); return SA; } public float LateralSurfaceAreaOfCuboid(float length, float width, float height) { LSA = 2 * height * (length + width); return LSA; } }

Within the Main program, we will create an instance of the above specified class and call the methods.

**JAVA CODE**

package SurfaceAreaPrograms; import java.util.Scanner; public class VolumeOfCuboidUsingClass { private static Scanner sc; public static void main(String[] args) { // LSA = Lateral Surface Area of a Cube, SA = Surface Area float length, width, height, SA, Volume, LSA; sc = new Scanner(System.in); System.out.println(" Please Enter the Length, Width & Height of a Cuboid : "); length = sc.nextFloat(); width = sc.nextFloat(); height = sc.nextFloat(); VolumeOfaCuboid vcd = new VolumeOfaCuboid(); SA = vcd.SurfaceAreaOfCuboid(length, width, height); Volume = vcd.VolumeOfCuboid(length, width, height); LSA = vcd.LateralSurfaceAreaOfCuboid(length, width, height); System.out.format(" The Surface area of a Cuboid = %.2f", SA); System.out.format("\n The Volume of a Cuboid = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cuboid = %.2f", LSA); } }

**OUTPUT**

**ANALYSIS**

**VolumeofACuboid Class Analysis:**

- First we declared a function
**VolumeofCuboid**with three argument. Within the Java Program to find Volume and Surface Area of Cuboid function, we are calculating the Volume of a Cuboid and returning the value - Next, we declared a function
**SurfaceAreaofCuboid**with three argument. Within the function, we are calculating the Surface Area of a Cuboid and returning the value - Next we declared a function
**LateralSurfaceAreaofCuboid**with three argument. Within the function, we are calculating the Lateral Surface Area of a Cuboid and returning the value

**TIP:** If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

**Main Class Analysis:**

In this Java Program to find Volume and Surface Area of Cuboid, First we created an instance / created an Object of the **VolumeofACuboid** Class

VolumeOfaCuboid vcd = new VolumeOfaCuboid();

Next, we are calling the **VolumeofCuboid** method by passing three arguments. This is the first method that we created with double data type and this method will calculate Volume of a Cuboid and return a value so, we are assigning the return value to volume variable.

Volume = vcd.VolumeOfCuboid(length, width, height);

Next, we are calling the **Surface****AreaofCuboid** method. This is the second method that we created with double data type and this method will calculate Surface Area of a Cuboid and return a value so, we are assigning the return value to SA variable.

SA = vcd.SurfaceAreaOfCuboid(length, width, height);

Next, we are calling the **Lateral****Surface****AreaofCuboid** method. This is the third method that we created with double data type and this method will calculate Lateral Surface Area of a Cuboid and return a value so, we are assigning the return value to LSA variable.

LSA = vcd.LateralSurfaceAreaOfCuboid(length, width, height);

Lastly we used following System.out.format statement to print the Volume of a Cuboid, Lateral Surface Area of a Cuboid and Surface Area of a Cuboid

System.out.format(" The Surface area of a Cuboid = %.2f", SA); System.out.format("\n The Volume of a Cuboid = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cuboid = %.2f", LSA);