Java program to calculate the volume of a cuboid. We can find the value of cuboid in so different ways. We do written the code in 3 different ways check out the table of contents so that you will get an idea. If you have any doubts related to the following java program to find the volume of cuboid, then do comment our dedicated team will help you out.
Table of contents :
Before we explain the code, a couple of lines about cuboid and math formula to find the volume.
What is Cuboid?
A: Convex Polyhedron which was bounded by 6 Quadrilateral Faces.
What is the Formula To Find the Volume of Cuboid?
A: Finding the volume is quite easy. Simple formula Is L X B X H. Most of the code will be base on the following Formula. Do check out 🙂
Sample Program # 1:
[wp_ad_camp_3]
Here Goes the Sample Program if you know the basics then skip the explanation part and move on to the second sample program to find the volume of a cuboid. # Do check out the Online execution and compiler tool just after the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; class VolumeOfCuboid { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the length of Cubiod:"); double l=s.nextDouble(); System.out.println("Enter the breadth of Cubiod:"); double b=s.nextDouble(); System.out.println("Enter height of Cubiod:"); double h=s.nextDouble(); double volume= l*b*h; System.out.println("Volume Of Cuboid is:" +volume); } } |
Output :
1 2 3 4 5 6 7 |
Enter the length of Cubiod: 5 Enter the breadth of Cubiod: 10 Enter height of Cubiod: 10 Volume Of Cuboid is:500.00 |
Here Goes the online implementation tool for the above sample program # 1. Enter the values by line by line. If you encounter any problems while executing the program, do let us know.
Here goes the another sample program.
Sample Program #2: To calculate a volume of cuboid ( Using command line arguments )
What are command line arguments?
[wp_ad_camp_2]
The console is an interface between the user and program. When a user enters the inputs on the console using commands, we sending the data as an argument to the main method in java that’s why in public static void main() we creating a string array to store values which work at executing time.
>> In simple words, it’s just an another form of method that was used back in 2000’s year where scanner function was not introduced. After all that a new JDK was updated further by a scanner function was implemented to solve the problems much more confidently – Read more about command line arguments with examples here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; class VolumeOfCuboid1 { public static void main(String args[]) { double l=Double.parseDouble(args[0]; double b=Double.parseDouble(args[1]); double h=Double.parseDouble(args[2]); double volume= l*b*h; System.out.println("Volume Of Cuboid is:" +volume); } } |
Output :
1 |
parse output : 500 |
Here goes the another sample Program # 3
Here we used the return invoked method if you have a basic idea about java programming you can easily understand. Just in case if anyone asks to write the program using return method. The following sample program will help you out.
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.Scanner; class VolumeOfCuboid { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the length of Cubiod:"); double l=s.nextDouble(); System.out.println("Enter the breadth of Cubiod:"); double b=s.nextDouble(); System.out.println("Enter height of Cubiod:"); double h=s.nextDouble(); double a=VolumeOfCuboid.Volume(l,b,h); System.out.println("Volume Of Cuboid is:" +a); } public static double Volume(double l,double b,double h) { double a= l*b*h; return a; } } |
Java Basic Tutorials: