Hi everyone, In this post, we will learn Java Program to calculate Volume of Cone. A cone is a three-dimensional shape in geometry that narrows smoothly from a flat base (usually circular base) to a point(which forms an axis to the Centre of the base) called the apex or vertex.
In the previous tutorial, you have learned Java Program to Calculate area and volume of cuboid. Now let’s find the volume of the cone.
The mathematical formula for finding the volume of the cone is as follows.
Volume of cone(V) = ⅓ πr2h cubic units
Where,
- π is a constant whose value is 3.1415 or 22/7.
- r is the radius of its circular base.
- h is the height from the vertex to the base.
Now let’s write the program.
Java Program to Calculate Volume of Cone
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.*; public class Cone { public static void main(String[] args) { double r, h, volume; Scanner in = new Scanner(System.in); // Asking the user to enter the radius and height of the cone System.out.println(" Enter the radius and height of the cone :"); r = in.nextDouble(); h = in.nextDouble(); volume = (Math.PI * r * r * h)/3; // Calculating volume of cone System.out.println(" Volume of Cone : " + volume +" cubic units"); } } |
What we did ?
- First of all we have imported the util package.
- Then created a class and named it Cone.
- 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 radius and height of the cone. Then read the value of radius and height of the cone entered by the user.
- After that calculated the volume of the cone using the formula volume = (Math.PI * r * r * h)/3
- Then finally displayed the value of volume of the cone on the screen.
Now let’s check the output of the above code.
Output
1 2 |
Enter the radius and height of the cone :2 4 Volume of Cone : 16.755160819145562 cubic units |
So this was all about Java Program to calculate volume of Cone tutorial. In the next tutorial, you will learn Java Program to calculate Surface Area and Volume of Sphere.
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 Area and Volume of Cuboid
- Java Program to calculate Total Surface Area and Curved Surface Area of cylinder