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!

Updated on: 2025-02-04T02:19:25+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements