Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to find volume of capsule
A capsule is a three-dimensional geometric figure that consists of a cylindrical body with hemispherical ends on both sides. The volume of a capsule can be calculated by adding the volume of the cylindrical part and the volume of the two hemispherical ends. In this tutorial, we will learn how to find the volume of a capsule in Python using different approaches.
Formula for Volume of Capsule
The volume of a capsule combines the volumes of its cylindrical and hemispherical parts ?
Volume of Capsule = Volume of cylinder + Volume of both hemispheres Volume of Capsule = ? × r² × h + (2/3) × ? × r³ + (2/3) × ? × r³ Volume of Capsule = ? × r² × h + (4/3) × ? × r³
Where:
r ? is the radius of the hemispherical ends
h ? is the height of the cylindrical body (excluding the hemispherical ends)
Using Direct Formula Approach
We can calculate the capsule volume directly using the formula: Volume = ? × r² × h + (4/3) × ? × r³.
import math
radius = 5
height = 10
# Calculate volume using the capsule formula
cylindrical_part = math.pi * radius**2 * height
hemispherical_part = (4/3) * math.pi * radius**3
volume = cylindrical_part + hemispherical_part
print(f"Radius: {radius} units")
print(f"Height: {height} units")
print(f"Volume: {volume:.2f} cubic units")
Radius: 5 units Height: 10 units Volume: 1309.00 cubic units
Using Function Approach
We can create a reusable function to calculate the capsule volume for different dimensions ?
import math
def calculate_capsule_volume(radius, height):
"""Calculate volume of capsule given radius and height"""
cylindrical_volume = math.pi * radius**2 * height
hemispherical_volume = (4/3) * math.pi * radius**3
return cylindrical_volume + hemispherical_volume
# Test with different dimensions
dimensions = [(5, 10), (7, 15), (3, 8)]
for r, h in dimensions:
volume = calculate_capsule_volume(r, h)
print(f"Radius: {r}, Height: {h} ? Volume: {volume:.2f} cubic units")
Radius: 5, Height: 10 ? Volume: 1309.00 cubic units Radius: 7, Height: 15 ? Volume: 3731.86 cubic units Radius: 3, Height: 8 ? Volume: 339.29 cubic units
Interactive Volume Calculator
Here's a more comprehensive calculator that breaks down the volume components ?
import math
def capsule_volume_breakdown(radius, height):
"""Calculate capsule volume with component breakdown"""
# Calculate individual components
cylindrical_volume = math.pi * radius**2 * height
hemispherical_volume = (4/3) * math.pi * radius**3
total_volume = cylindrical_volume + hemispherical_volume
return {
'cylindrical': cylindrical_volume,
'hemispherical': hemispherical_volume,
'total': total_volume
}
# Example calculation
radius = 6
height = 12
result = capsule_volume_breakdown(radius, height)
print(f"Capsule with radius {radius} and height {height}:")
print(f"Cylindrical part: {result['cylindrical']:.2f} cubic units")
print(f"Hemispherical parts: {result['hemispherical']:.2f} cubic units")
print(f"Total volume: {result['total']:.2f} cubic units")
Capsule with radius 6 and height 12: Cylindrical part: 1357.17 cubic units Hemispherical parts: 904.78 cubic units Total volume: 2261.95 cubic units
Comparison of Methods
| Method | Reusability | Readability | Best For |
|---|---|---|---|
| Direct Formula | Low | Simple | One-time calculations |
| Function Approach | High | Clean | Multiple calculations |
| Breakdown Function | High | Detailed | Understanding components |
Conclusion
The capsule volume formula combines cylindrical and hemispherical components: ? × r² × h + (4/3) × ? × r³. Use functions for reusability and better code organization when calculating multiple capsule volumes.
