In the previous article we have discussed Java Program to Find Volume of Cylinder
In this article we will discuss about how to find volume of cone.
Program to Find Volume of Cone
Before going into the program, let’s see how we find the volume of cone.
Formula for Volume of Cone = pie * (r*r) * h/3
Where,
- ‘
r
‘ represents radius of cone. - ‘
h
‘ represents height of cone.
Let’s see different ways to find volume of cone.
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Approach:
- Take user input or declare the values for height and radius of the cone and store it in double variable say
h
andr
respectively. - Then using the formula find volume of cone and store it in a variable say
volume
. - Then display the result.
Method-1: Java Program to Find Volume of Cone By Using Static Value
import java.util.*; public class Main { public static void main(String args[]) { //Scanner class object created Scanner s= new Scanner(System.in); //radius declared double r=3.3; //height declared double h=6.6; //pie value declared double pie=3.14285714286; //calulating volume of cone using formula double volume=pie*(r*r)*h/3; System.out.println("Volume of cone : "+volume); } }
Output: Volume of cone : 75.29657142863987
Method-2: Java Program to Find Volume of Cone By User Input Value
import java.util.*; public class Main { public static void main(String args[]) { //Scanner class object created Scanner s= new Scanner(System.in); //Taking radius input from user System.out.println("Enter radius of cone : "); double r=s.nextDouble(); //Taking height input from user System.out.println("Enter height of cone : "); double h=s.nextDouble(); double pie=3.14285714286; //calulating volume of cone using formula double volume=pie*(r*r)*h/3; System.out.println("Volume of cone : "+volume); } }
Output: Enter radius of cone : 3.3 Enter height of cone 6.6 Volume of cone : 75.29657142863987
Method-3: Java Program to Find Volume of Cone By User Defined Method
import java.util.*; public class Main { public static void main(String args[]) { //Scanner class object created Scanner s= new Scanner(System.in); //Taking radius input from user System.out.println("Enter radius of cone : "); double r=s.nextDouble(); //Taking height input from user System.out.println("Enter height of cone : "); double h=s.nextDouble(); //calling the findVolume() method findVolume(r,h); } //user defined method //findVolume() method to find volume of cone public static void findVolume(double r, double h) { //pie value declared double pie=3.14285714286; //calulating volume of cone using formula double volume=pie*(r*r)*h/3; System.out.println("Volume of cone : "+volume); } }
Output: Enter radius of cone : 3.3 Enter height of cone 6.6 Volume of cone : 75.29657142863987
Related Java Programs: