Java Program to Find Volume and Surface Area of Cuboid

In the previous article, we have seen Java Program to Find Volume of Ellipsoid

In this article we are going to see how to find the volume of cuboid using Java programming language.

Java Program to Find Volume and Surface Area of Cuboid

Before Jumping into the program directly let’s see how we can find the volume of cuboid.

Explanation:

Formula to find volume of cuboid = l * b* h
Formula to find Surface area of cuboid = 2(lb + bh + hl)

Where,

  • l represents length of the cuboid
  • b represents breadth of the cuboid
  • h represents height of the cuboid

Example:

Let 
length of cuboid i.e. l= 1
Breadth i.e. b= 2
Height i.e. h= 3
So, Volume of cuboid = l*b*h = 1*2*3 = 6
Surface area of cuboid = 2(lb+bh+hl) = 2((1*2) + (2*3) +( 3*1)) = 2(2+6+3) = 22

Let’s see different ways to find volume and surface area of cuboid.

Method-1: Java Program to Find Volume and Surface Area of Cuboid By Using Static Value

Approach:

  • Declare an integer variable say ‘l’ and assign the value to it which holds length of the cuboid.
  • Declare an integer variable say ‘b’ and assign the value to it which holds breadth of the cuboid.
  • Declare an integer variable say ‘h’ and assign the value to it which holds height of the cuboid.
  • Find the volume of cuboid using the formula l*b*h and find the Surface area of cuboid using the formula  2(lb+bh+hl)
  • Print the result.

Program:

class Main
{
    public static void main(String [] args)
    {
        //length, breadth, height value declared
        int l = 1;
        int b = 2;
        int h = 3;
        //find volume
        double vol =  l*b*h;
        //find area
        double area = 2*((l*b) + (b*h) + (h*l));
        System.out.println("The volume of cuboid is: " + vol);
        System.out.println("The area of cuboid is: " + area);
    }
}

Output:

The volume of cuboid is: 6.0
The area of cuboid is: 22.0

Method-2: Java Program to Find Volume and Surface Area of Cuboid By Using User Input Value

Approach:

  • Declare an integer variable say ‘l’ and take value as user input, which holds length of the cuboid.
  • Declare an integer variable say ‘b’ and take value as user input, which holds breadth of the cuboid.
  • Declare an integer variable say ‘h’ and take value as user input, which holds height of the cuboid.
  • Find the volume of cuboid using the formula l*b*h and find the Surface area of cuboid using the formula  2(lb +bh+hl)
  • Print the result.

Program:

import java.util.*;

class Main
{
    public static void main(String [] args)
    {
        //Scanner class object created
        Scanner s = new Scanner(System.in);
        //taking input of length, breadth, height value 
        System.out.println("Enter the length of cuboid:");
        int l = s.nextInt();
        System.out.println("Enter the breadth of cuboid:");
        int b = s.nextInt();
        System.out.println("Enter the heigth of cuboid:");
        int h = s.nextInt();

        //find volume
        double vol =  l*b*h;
        //find area
        double area = 2*((l*b) + (b*h) + (h*l));
        System.out.println("The volume of cuboid is: " + vol);
        System.out.println("The area of cuboid is: " + area);
    }
}

Output:

Enter the length of cuboid:
3
Enter the breadth of cuboid:
4
Enter the heigth of cuboid:
5
The volume of cuboid is: 60.0
The area of cuboid is: 94.0

Method-3: Java Program to Find Volume and Surface Area of Cuboid By Using User Defined Method

Approach:

  • Declare an integer variable say ‘l’ and take value as user input, which holds for length of the cuboid.
  • Declare an integer variable say ‘b’ and take value as user input, which holds for breadth of the cuboid.
  • Declare an integer variable say ‘h’ and take value as user input, which holds for height of the cuboid.
  • Call the method findValue() and pass l, b, h as parameter.
  • Inside the method, find the volume of cuboid using the formula l*b*h and find the Surface area of cuboid using the formula  2(lb+bh+hl)
  • Print the result.

Program:

import java.util.*;

class Main
{
    public static void main(String [] args)
    {
        //Scanner class object created
        Scanner s = new Scanner(System.in);
        //taking input of length, breadth, height value 
        System.out.println("Enter the length of cuboid:");
        int l = s.nextInt();
        System.out.println("Enter the breadth of cuboid:");
        int b = s.nextInt();
        System.out.println("Enter the heigth of cuboid:");
        int h = s.nextInt();
        //calling the method findValue() tofind volume and area of Cuboid
        findValue(l,b,h);
    }

    public static void findValue(int l,int b,int h)
    {
        //find volume
        double vol =  l*b*h;
        //find area
        double area = 2*((l*b) + (b*h) + (h*l));
        System.out.println("The volume of cuboid is: " + vol);
        System.out.println("The area of cuboid is: " + area);
    }
}

Output:

Enter the length of cuboid:
4
Enter the breadth of cuboid:
5
Enter the heigth of cuboid:
6
The volume of cuboid is: 120.0
The area of cuboid is: 148.0

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Related Java Articles: