In the previous article we have discussed Java Program to Find Area and Perimeter of Trapezium
In this article we will discuss about how to find volume of cylinder.
Program to Find Volume of Cylinder
Before going into the program, let’s see how we find the volume of cylinder.
Formula for Volume of Cylinder = pie * (r*r) * h
Where,
- ‘
r
‘ represents the radius of cylinder. - ‘
h
‘ represents the height of cylinder.
Let’s see different ways to find volume of cylinder.
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Approach:
- Take input/declare the height, radius of cylinder.
- Then calculate the volume by using the formula.
Method-1: Java Program to Find Volume of Cylinder By Using Static Value
import java.util.*; public class Main { public static void main(String args[]) { //height of cylinder declared int height=8; //radius of cylinder declared int radius=5; //pie vlaue declared double pie=3.14285714286; //calculating volume of cylinder double volume = pie * radius * radius * height; System.out.println("Volume of cylinder="+volume); } }
Output: Volume of cylinder=628.5714285720001
Method-2: Java Program to Find Volume of Cylinder By User Input Value
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); // Take height input from user System.out.println("Enter Height of Cylinder"); double height = sc.nextDouble(); // Take radius input from user System.out.println("Enter radius of Cylinder : "); double radius = sc.nextDouble(); //pie vlaue declared double pie=3.14285714286; //calculating volume of cylinder double volume = pie * radius * radius * height; System.out.println("Volume of cylinder="+volume); } }
Output: Enter Height of Cylinder : 8 Enter radius of Cylinder : 5 Volume of cylinder=628.5714285720001
Method-3: Java Program to Find Volume of Cylinder By User Defined Method
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); // Take height input from user System.out.println("Enter Height of Cylinder : "); double height = sc.nextDouble(); // Take radius input from user System.out.println("Enter radius of Cylinder : "); double radius = sc.nextDouble(); //findVolume() method called to find volume of cylinder findVolume(height,radius); } //findVolume() method public static void findVolume(double height,double radius) { //pie vlaue declared double pie=3.14285714286; //calculating volume of cylinder double volume = pie * radius * radius * height; System.out.println("Volume of cylinder="+volume); } }
Output: Enter Height of Cylinder : 8 Enter radius of Cylinder : 5 Volume of cylinder=628.5714285720001
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Related Java Programs: