Java Program to Find the Surface area and Volume of Cuboid



In this article, we will understand how to compute the surface area and volume the cuboid. Cuboid is a three-dimensional object with six faces of rectangle shape which means it has sides of different length and breadth. The difference between a cube and cuboid is that a cube has equal length, height and breadth whereas in cuboids these three are not same

The surface area of cuboid is calculated using the formula −

2*( length *width + width* height + height*length)

The area of the cuboid is calculated using the formula −

length*width*height

Below is a demonstration of the same −

Input

Suppose our input is −

Length= 6;
Width= 7;
Height= 8;

Output

The desired output would be −

Volume Of the Cuboid is : 336.0
Surface area Of the Cuboid is : 292.0

Algorithm

Step 1 - START
Step 2 - Declare five double values namely my_length, my_width, my_height, my_volume, my_surface_area
Step 3 - Read the required values from the user/ define the values
Step 4 - Use the formula 2*( length *width + width* height + height*length) to calculate the surface area of cuboid
Step 5 - Use the formula length*width*height to calculate the area of the cuboid
Step 6 - Display the result
Step 7 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class VolumeOfCuboid{
   public static void main(String args[]){
      double my_length, my_width, my_height, my_volume, my_surface_area;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.println("Enter the length of Cubiod:");
      my_length=my_scanner.nextDouble();
      System.out.println("Enter the width of Cubiod:");
      my_width=my_scanner.nextDouble();
      System.out.println("Enter height of Cubiod:");
      my_height=my_scanner.nextDouble();
      my_volume= my_length*my_width*my_height;
      System.out.println("The volume Of the Cuboid is :" +my_volume);
      my_surface_area =2*( my_length *my_width + my_width* my_height + my_height*my_length);
      System.out.println("The surface area Of the Cuboid is : " +my_surface_area);
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the length of Cubiod:
6
Enter the width of Cubiod:
7
Enter height of Cubiod:
8
The volume Of the Cuboid is : 336.0
The surface area Of the Cuboid is : 292.0

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class VolumeOfCuboid{
   public static void main(String args[]){
      double my_length, my_width, my_height, my_volume, my_surface_area;
      my_length= 6;
      my_width= 7;
      my_height= 8;
      my_volume= my_length*my_width*my_height;
      System.out.println("The volume Of the Cuboid is:" +my_volume);
      my_surface_area =2*( my_length *my_width + my_width* my_height + my_height*my_length);
      System.out.println("The surface area Of the Cuboid is:" +my_surface_area);
   }
}

Output

The volume Of the Cuboid is:336.0
The surface area Of the Cuboid is:292.0

Advertisements