Hello everyone, in this tutorial, we will learn Java Program to calculate Area and Volume of Cuboid. Have you checked the previous tutorial, Java Program to calculate Area and Volume of Cube? If not, then now you can check it out.
Calculating the area and volume of a cuboid in Java is not difficult. It can be done in just a few lines of code. But first, we need to know the mathematical formula to find the area and volume of the cuboid. So let’s know this.
Area of cuboid = 2 (lb + bh + lh)
Where,
- l is length of the cuboid.
- b is breadth of the cuboid.
- h is height of the cuboid.
Volume of cuboid =l×b×h
Where,
- l is length of the cuboid
- b is breadth of the cuboid
- h is height of the cuboid.
Now let’s write the program. Here is the complete source code of this program, and explanations are given after the program.
Java Program to Calculate Area and Volume of Cuboid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.*; public class Cuboid { public static void main(String[] args) { double l, b, h, area, volume; Scanner in = new Scanner(System.in); // Asking the user to enter the length, breadth and height of the cuboid System.out.println(" Enter the length, breadth and height of the cuboid :"); l = in.nextDouble(); b = in.nextDouble(); h = in.nextDouble(); area = l * b * h; // Calculating area of cuboid volume = 2*(l*b + b*h + l*h); // Calculating volume of cuboid System.out.println(" Area of Cuboid : " + area +" square units"); System.out.println(" Volume of Cuboid : " + volume +" cubic unit); } } |
What we did ?
- First of all we have imported the util package.
- Then created a class and named it Cuboid.
- After that started main() method of the program.
- Then inside main() method declared variables.
- Then created object of the Scanner class.
- Now asked the user to enter the length, breadth, and height of the cuboid. Then read the value of length, breadth, and height of the cuboid entered by the user.
- Then calculated area of the cuboid using the formula area = l * b * h.
- After that calculated volume of the cuboid using the formula volume = 2*(l*b + b*h + l*h).
- Then finally displayed the value of area and volume of the cuboid on the screen.
Now let’s check the output of the above code.
Output
1 2 3 4 |
Enter the length, breadth and height of the cuboid : 1 3 4 Area of Cuboid : 12.0 square units Volume of Cuboid : 38.0 cubic unit |
So guys, Java Program to calculate Area and Volume of Cuboid tutorial is completed. In the next tutorial, you will learn Java Program to calculate Volume of Cone.
Related Tutorials…
- Java Program to Calculate Area and Circumference of Circle
- Java Program to calculate Area of Triangle
- Java Program to calculate Area of Triangle using heron’s formula
- Java Program to calculate Area and Perimeter of Right Triangle
- Java Program to calculate Area and Perimeter of Rectangle
- Java Program to calculate Area and Perimeter of Square
- Java Prorgram to calculate Area and Perimeter of Parallelogram
- Java Program to calculate Area of Trapezium
- Java Program to calculate Area and Volume of Cube
- Java Program to calculate Total Surface Area and Curved Surface Area of cylinder
- Java Program to calculate Surface Area and Volume of Sphere