Java Program to Find Volume and Surface Area of Cuboid

Here is a Java program to calculate the Surface area and volume of a cuboid. In this Java program, we have to find the volume and total surface area of a cuboid, given the length, width and height of a cuboid.
A cuboid is a three dimensional object having six rectangular sides. A cuboid has twelve edges, six rectangular faces and eight vertices. Each face of a cuboid is a rectangle and all three adjacent faces are perpendicular to each other.

Example of Cuboid : Match Box
Volume of Cuboid
Volume of a cuboid is the amount of three dimensional space occupied by a cuboid. The volume of cuboid is measured in cubic units. To calculate the volume of a cube we should know the length, width and height of a cuboid.
Volume of Cuboid = L X W X H
Where, L is the length of cuboid, W is the width of cuboid and H is the height of cuboid.
Total Surface Area of Cuboid
The total surface area of cuboid is the total area on the outer side of a cuboid. A cuboid has six rectangular surfaces of different length, width and height.
Total Surface area of Cuboid = 2 X (LXW + LXH + WXH)
Where, L is the length of cuboid, W is the width of cuboid and H is the height of cuboid.

Java program to find the volume and surface area of cuboid

In this java program, we first take length, width and height of cuboid as input from user and then calculate volume and surface area using above mentioned mathematical expressions.

package com.tcc.java.programs;

import java.util.Scanner;

public class VolumeOfCuboid {

    public static void main(String[] args) {
        double length, width, height, volume, surfaceArea;
        Scanner scanner;
        scanner = new Scanner(System.in);
        // Take input from user
        System.out.println("Enter Length of Cuboid");
        length = scanner.nextDouble();

        System.out.println("Enter Width of Cuboid");
        width = scanner.nextDouble();

        System.out.println("Enter Height of Cuboid");
        height = scanner.nextDouble();

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

        volume = length * width * height;

        System.out.format("Surface Area of Cuboid 
           = %.3f\n", surfaceArea);
        System.out.format("Volume of Cuboid 
           = %.3f\n", volume);
    }
}
Output
Enter Length of Cuboid
2
Enter Width of Cuboid
3
Enter Height of Cuboid
4
Surface Area of Cuboid = 52.000
Volume of Cuboid = 24.000

Java program to find the total surface area and volume of cuboid using function

This java program is similar to above program except here we are using two user defined functions "getSurfaceArea" and "getVolume" functions to calculate total surface area and volume of cuboid respectively. Both functions takes length, width and height of cuboid as input parameter.

package com.tcc.java.programs;

import java.util.Scanner;

public class VolumeOfCuboidFunction {

    public static void main(String[] args) {
        double length, width, height;
        Scanner scanner;
        scanner = new Scanner(System.in);
        // Take input from user
        System.out.println("Enter Length of Cuboid");
        length = scanner.nextDouble();

        System.out.println("Enter Width of Cuboid");
        width = scanner.nextDouble();

        System.out.println("Enter Height of Cuboid");
        height = scanner.nextDouble();

        System.out.format("Surface Area of Cuboid = %.3f\n", 
            getSurfaceArea(length, width, height));
        System.out.format("Volume of Cuboid = %.3f\n", 
            getVolume(length, width, height));
    }

    public static double getSurfaceArea(double length, double width, 
        double height) {
        return 2 * (length * width + width * height + height * length);
    }

    public static double getVolume(double length, double width, double height) {
        /* Volume of Cuboid = length*width*height */
        return length * width * height;
    }
}
Output
Enter Length of Cuboid
5
Enter Width of Cuboid
6
Enter Height of Cuboid
7
Surface Area of Cuboid = 214.000
Volume of Cuboid = 210.000

Recommended Posts
Java Program to Calculate Surface Area and Volume of Cube
Java Program to Calculate Volume and Surface Area of Cone
Java Program to Calculate Volume of Sphere
Java Program to Find Area of Right Angled Triangle
Java Program to Find Area Of Trapezoid
Java Program to Find Area Of Triangle
Java Program to find Area Of Equilateral Triangle
Java Program to calculate area and circumference of circle
Java Program to Find Surface Area and Volume of Cylinder
Java Program to Find Area and Perimeter of Rectangle
Java Program to Convert Celsius to Fahrenheit
All Java Programs