
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
Python program to find volume of capsule
What is Capsule and Its Volume?
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 present on both sides of the cylindrical part. In this tutorial, we are going to discuss how to find the volume of a given capsule in Python using different approaches.
Formula for Volume of Capsule
The formula for the volume of a capsule is as follows:
Volume of Capsule = Volume of cylinder + Volume of both hemisphere 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).
Example 1
-
Input:
Radius = 5 units
Height = 10 units - Output: Volume = 1570.8 cubic units
Explanation
Using the formula to calculate the volume:
Volume = ? à r2 à h + (4/3) à ? à r³
Volume = 785.4 + 523.6
Volume = 1570.8 cubic units
Example 2
-
Input:
Radius = 7 units
Height = 15 units - Output: Volume = 4311.97 cubic units
Explanation
Using the formula to calculate the volume:
Volume = ? à r2 à h + (4/3) à ? à r³
Volume = 2309.4 + 2002.57
Volume = 4311.97 cubic units
How to Find the Volume of Capsule in Python?
Below are different approaches to calculating the volume of a capsule in Python:
- Using Direct Formula Approach
- Using Function
Using Direct Formula Approach
We use the direct formula approach to calculate the volume of the capsule in Python. The formula for the volume of a capsule is: Volume = ? à r² à h + (4/3) à ? à r³.
Steps for Implementation
- Take the radius and height as input parameters.
- Calculate the volume using the formula.
- Print the result.
Implementation Code
import math radius = 5 height = 10 volume = math.pi * radius**2 * height + (4/3) * math.pi * radius**3 print(f"The volume of the capsule with radius {radius} and height {height} is: {volume:.2f} cubic units")
Output
The volume of the capsule with radius 5 and height 10 is: 1309.00 cubic units
Time Complexity: O(1)
Space Complexity: O(1)
Using Function
We will use a function to calculate the volume of the capsule. The logic and formula for calculating the volume remain the same, but we encapsulate the calculation in a reusable function.
Steps for Implementation
- Define a function to calculate the volume of the capsule using the formula.
- Pass the input values (radius and height) to the function.
- Return the result and print it.
Implementation Code
import math def calculate_volume(radius, height): cylindrical_volume = math.pi * radius**2 * height hemispherical_volume = (4/3) * math.pi * radius**3 return cylindrical_volume + hemispherical_volume radius = 5 height = 10 volume = calculate_volume(radius, height) print(f"The volume of the capsule with radius {radius} and height {height} is: {volume:.1f} cubic units")
Output
The volume of the capsule with radius 5 and height 10 is: 1309.0 cubic units
Time Complexity: O(1)
Space Complexity: O(1)