Calculate Volume of Capsule in C++



A capsule is a three-dimensional geometric shape 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 C++ 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 hemispheres

where:
r: The radius of the hemispherical ends.
h: 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 = ? × r² × 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 = ? × r² × h + (4/3) × ? × r³
Volume = 2309.4 + 2002.57
Volume = 4311.97 cubic units

How to Find the Volume of Capsule in C++?

Below are different approaches to calculate the volume of a capsule in C++:

Using the Direct Formula Approach

We use the direct formula approach to calculate the volume of the capsule in C++. 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.
  • Print the result.
  • Calculate the volume using the formula.

Implementation Code

#include<bits/stdc++.h>
#include<cmath>
using namespace std;

int main() {
  double radius = 5;
  double height = 10;

  double volume = M_PI * pow(radius, 2) * height + (4.0 / 3) * M_PI * pow(radius, 3);

  cout << "The volume of the capsule with radius " << radius << " and height " << height;
  cout << " is: " << fixed << setprecision(2) << volume << " cubic units" << endl;

  return 0;
}

Output:

The volume of the capsule with radius 5.0 and height 10.0 is: 1309.00 cubic units

Time Complexity: O(1)
Space Complexity: O(1)

Using a Function

In this approach, we use a function, the logic is the same as the above approach. We encapsulate the logic for calculating the volume of the capsule into a reusable function. The formula remains the same, but this approach improves the reusability of the code.

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

#include<bits/stdc++.h>
#include<cmath>
#include<iomanip>

using namespace std;

double calculateVolume(double radius, double height) {
  double cylindricalVolume = M_PI * pow(radius, 2) * height;
  double hemisphericalVolume = (4.0 / 3) * M_PI * pow(radius, 3);
  return cylindricalVolume + hemisphericalVolume;
}
int main() {
  double radius = 5;
  double height = 10;
  double volume = calculateVolume(radius, height);

  cout << "The volume of the capsule with radius " << radius << " and height " << height;
  cout << " is: " << fixed << setprecision(2) << volume << " cubic units" << endl;

  return 0;
}

Output:

The volume of the capsule with radius 5.0 and height 10.0 is: 1309.00 cubic units

Time Complexity: O(1)
Space Complexity: O(1)

By using these approaches, you can easily calculate the volume of a capsule in C++ while keeping your code clean and modular. Choose the approach that best suits your requirements!

Updated on: 2025-01-27T13:15:57+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements