Find Surface Area and Volume of Cuboid in Java



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

Problem Statement

Write a program in Java to find a cuboid's surface area and volume. Below is a demonstration of the same ?

Input

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

Output

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

Different approaches

Following are the different approaches to find the surface area and volume of cuboid ?

Using user input

Here, the input is being entered by the user based on a prompt ?

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

Using predefined input

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

Code explanation

The surface area of the 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 ?

At the end, we will put the values of length, width, and height in the formula and will get the output after calculation.

Updated on: 2024-11-13T12:20:52+05:30

595 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements