Java program to find the volume of a cylinder with examples. Here, we shared three to four basic programs about to calculate the volume of a cylinder. If you have any doubts related to this programming then do comment at the end of the post we are glad to help you out – check java program to find the area of a triangle
Table of contents :
- Example # 1 – A simple java program universally applicable.
- Example # 2 – Using a command line argument # universally applicable.
- Example # 3 – Using a static method – # Universally applicable.
# check out the online compiler and execution tool for each example program #
What is a cylinder?
A geometrical figure with straight parallel sides and a circular cross-section. For detailed information check out the below definition.
What is the formula to find out the volume of a cylinder?
A : The basic formula in order to calculate the volume of cylinder as follows :
Volume: π × r² × h
[wp_ad_camp_3]
Here is the sample program # 1 : # The following java program is tested and executed in cmd. You can also check execute in the below compiler tool. #calculate volume of prism java code #
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; class VolumeOfCylinder { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the radius:"); double r=s.nextDouble(); System.out.println("Enter the height:"); double h=s.nextDouble(); double volume=((22*r*r*h)/7); System.out.println("volume of Cylinder is: " +volume); } } |
Output :
1 2 3 4 5 |
Enter the radius: 5 Enter the height: 10 volume of Cylinder is:785.40 |
Online execution tool for the above program #example -1
Here is the complete explanation of above java code. If you at the beginning stage, then you can check out the below tutorial. so that you will get an idea. If you know how the code works , then you can check out the rest of the examples with online compiler and execution tool.
1 |
import java.util.Scanner; |
– System read the input variables and pass the necessary methods to a new scanner.
1 |
class VolumeOfCylinder |
– It is a blueprint where objects are created within a class.
1 |
public static void main(String args[]) |
– The main function , convert the given string as an argument into different formats as like double . int , float.
1 |
Scanner s= new Scanner(System.in); |
– Read the input values, where system.in can read the values from your system(import java.util.scanner) JDK file.
1 |
System.out.println("Enter the radius:"); |
– Display the output values , in that whatever you were written.
1 |
double r=s.nextDouble(); |
– Where double is a datatype , which can store the particular values.
1 |
double volume=((22*r*r*h)/7); |
– The formula to find out the volume of a cylinder in java.
1 |
System.out.println("volume of Cylinder is: " +volume); |
– Output displayed here.
[wp_ad_camp_2]
So, you got an idea how the above java program works to find the volume of a cylinder. Below is another example method to calculate the volume of a cylinder. The difference between the above example -1 when compared with the below example -2 is nothing but just a couple of syntax lines has been changed. The little difference is as, whereas scanner function is used in order to retrieve the input variables and store in a particular location until the program executes. Whereas here, ‘parse’ method is used to directly retrieve the details from the screen itself.
Compiler method in # CMD: java filename.java
Execution method in #CMD: java classname arguments
Here is the Example program # 2 – using command line argument
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; class VolumeOfCylinder1 { public static void main(String args[]) { double r=Double.parseDouble(args[0]); double h=Double.parseDouble(args[1]); double volume=((22*r*r*h)/7); System.out.println("volume of Cylinder is: " +volume); } } |
Here is the online execution tool for the above program to calculate the volume of a cylinder.
Here is another example method #3 to calculate the volume. In the following method, we used a return invoked method. Check it out.
Example method # 3
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 28 29 30 31 |
import java.util.Scanner; class VolumeOfCylinder { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the radius:"); double r=s.nextDouble(); System.out.println("Enter the height:"); double h=s.nextDouble(); double a=VolumeOfCylinder.Volume(r,h); System.out.println("Volume Of Cylinder is:" +a); } public static double Volume(double r,double h) { double a=((22*r*r*h)/7); return a; } } } |
# java program to calculate the volume of cylinder ends #