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

  1. Take the radius and height as input parameters.
  2. Calculate the volume using the formula.
  3. 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

  1. Define a function to calculate the volume of the capsule using the formula.
  2. Pass the input values (radius and height) to the function.
  3. 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)

Updated on: 2025-01-15T18:53:14+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started