
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Volume of a Cylinder in Java
A cylinder is a three-dimensional geometric shape with two parallel circular bases connected by a curved surface. The volume of a cylinder can be calculated using a mathematical formula that considers its radius and height.
Problem Description
In this tutorial, we are going to discuss how to find the volume of a given cylinder in Java using different approaches.Formula for Volume of Cylinder
The formula for the volume of a cylinder is as follows:
Volume of Cylinder = ? à r² à h
where:
- r: The radius of the circular base.
- h: The height of the cylindrical body.
Example 1
Input: Radius = 5 units Height = 10 units Output: Volume = 785.4 cubic units Explanation: Using the formula to calculate the volume: Volume = ? à 5² à 10 Volume = 785.4 cubic units
Example 2
Input: Radius = 7 units Height = 15 units Output: Volume = 2309.4 cubic units Explanation: Using the formula to calculate the volume: Volume = ? à 7² à 15 Volume = 2309.4 cubic units
How to Find the Volume of a Cylinder in Java?
Below are different approaches to calculate the volume of a cylinder in Java:
Using the Direct Formula Approach
We use the direct formula approach in Java to calculate the volume of the cylinder:
Volume = ? à r² à h
Steps for Implementation
- Take the radius and height as input parameters.
- Calculate the volume using the formula.
- Print the result.
Implementation Code
import java.text.DecimalFormat; public class CylinderVolume { public static void main(String[] args) { double radius = 5; double height = 10; double volume = Math.PI * Math.pow(radius, 2) * height; DecimalFormat df = new DecimalFormat("0.00"); System.out.println("The volume of the cylinder with radius " + radius + " and height " + height + " is: " + df.format(volume) + " cubic units"); } }
Output:
The volume of the cylinder with radius 5.0 and height 10.0 is: 785.40 cubic units
Time Complexity:
O(1)
Space Complexity:
O(1)
Using a Function
In this approach, we encapsulate the logic for calculating the volume of the cylinder into a reusable function.
Steps for Implementation
- Define a function to calculate the volume of the cylinder using the formula.
- Pass the input values (radius and height) to the function.
- Return the result and print it.
Implementation Code
import java.text.DecimalFormat; public class CylinderVolumeFunction { static double calculateVolume(double radius, double height) { return Math.PI * Math.pow(radius, 2) * height; } public static void main(String[] args) { double radius = 5; double height = 10; double volume = calculateVolume(radius, height); DecimalFormat df = new DecimalFormat("0.00"); System.out.println("The volume of the cylinder with radius " + radius + " and height " + height + " is: " + df.format(volume) + " cubic units"); } }
Output:
The volume of the cylinder with radius 5.0 and height 10.0 is: 785.40 cubic units
Time Complexity:
O(1)
Space Complexity:
O(1)
By using these approaches, you can easily calculate the volume of a cylinder in Java while keeping your code clean and modular. Choose the approach that best suits your requirements!