cyberangles blog

Program for Volume and Surface Area of Frustum of Cone

A frustum of a cone is a portion of a cone that lies between two parallel planes cutting it. Calculating its volume and surface area is an important task in geometry and has applications in various fields like engineering and architecture. In this blog, we'll explore how to write a program (using Python as an example language) to compute these values.

2026-07

Table of Contents#

  1. Formulas for Volume and Surface Area of Frustum of Cone
  2. Python Program Implementation
  3. Common Practices in Coding
  4. Example Usage
  5. Best Practices
  6. References

1. Formulas for Volume and Surface Area of Frustum of Cone#

  • Volume (V): [V=\frac{1}{3}\pi h (R^{2}+r^{2}+Rr)] where (h) is the height of the frustum, (R) is the radius of the larger base, and (r) is the radius of the smaller base.
  • Lateral (Curved) Surface Area (LSA): [LSA = \pi l (R + r)] where (l=\sqrt{h^{2}+(R - r)^{2}}) is the slant height of the frustum.
  • Total Surface Area (TSA): [TSA=\pi l (R + r)+\pi R^{2}+\pi r^{2}]

2. Python Program Implementation#

import math
 
def frustum_volume(h, R, r):
    """
    Calculate the volume of the frustum of a cone.
 
    Args:
    h (float): Height of the frustum
    R (float): Radius of the larger base
    r (float): Radius of the smaller base
 
    Returns:
    float: Volume of the frustum
    """
    volume = (1/3)*math.pi*h*(R**2 + r**2 + R*r)
    return volume
 
def frustum_lsa(h, R, r):
    """
    Calculate the lateral surface area of the frustum of a cone.
 
    Args:
    h (float): Height of the frustum
    R (float): Radius of the larger base
    r (float): Radius of the smaller base
 
    Returns:
    float: Lateral surface area of the frustum
    """
    l = math.sqrt(h**2+(R - r)**2)
    lsa = math.pi*l*(R + r)
    return lsa
 
def frustum_tsa(h, R, r):
    """
    Calculate the total surface area of the frustum of a cone.
 
    Args:
    h (float): Height of the frustum
    R (float): Radius of the larger base
    r (float): Radius of the smaller base
 
    Returns:
    float: Total surface area of the frustum
    """
    l = math.sqrt(h**2+(R - r)**2)
    lsa = math.pi*l*(R + r)
    tsa = lsa+math.pi*R**2+math.pi*r**2
    return tsa

3. Common Practices in Coding#

  • Function Definitions:
    • Each functionality (volume, lateral surface area, total surface area) is encapsulated in a separate function. This makes the code modular and easier to understand and maintain. For example, if you want to change the formula for calculating the volume in the future, you only need to modify the frustum_volume function.
  • Input Validation:
    • In a more robust implementation, we should add input validation. For instance, we can check if the height (h), radius (R), and radius (r) are positive values. In Python, we can use if statements to raise appropriate errors.
def frustum_volume(h, R, r):
    if h <= 0 or R <= 0 or r <= 0:
        raise ValueError("Height and radii must be positive values")
    volume = (1/3)*math.pi*h*(R**2 + r**2 + R*r)
    return volume
  • Documentation:
    • As shown in the code above, each function has a docstring. Docstrings are used to explain what the function does, what its inputs are, and what it returns. This is extremely helpful for other developers (or even yourself in the future) who might use or modify the code.

4. Example Usage#

# Example usage
h = 5  # height
R = 4  # radius of larger base
r = 2  # radius of smaller base
 
volume_result = frustum_volume(h, R, r)
lsa_result = frustum_lsa(h, R, r)
tsa_result = frustum_tsa(h, R, r)
 
print(f"Volume of the frustum: {volume_result}")
print(f"Lateral Surface Area of the frustum: {lsa_result}")
print(f"Total Surface Area of the frustum: {tsa_result}")

When you run this code, it will output the calculated values for the volume, lateral surface area, and total surface area of the frustum with the given dimensions.

5. Best Practices#

  • Testing:
    • Write unit tests for each function. In Python, you can use the unittest module. For example:
import unittest
 
class TestFrustum(unittest.TestCase):
    def test_frustum_volume(self):
        h = 3
        R = 2
        r = 1
        expected_volume = (1/3)*math.pi*3*(2**2 + 1**2 + 2*1)
        self.assertEqual(frustum_volume(h, R, r), expected_volume)
 
if __name__ == '__main__':
    unittest.main()

This ensures that your functions work as expected and helps catch any bugs introduced during code changes.

  • Code Formatting:
    • Follow a consistent code formatting style. In Python, PEP 8 is the standard. Tools like autopep8 can be used to automatically format your code according to PEP 8 guidelines. For example, proper indentation (using 4 spaces per level), line length (keeping lines under 79 characters), etc., make the code more readable.

6. References#

  • Mathematical Formulas:
    • The formulas used in this blog are based on standard geometric principles. You can refer to geometry textbooks like "Geometry Revisited" by H.S.M. Coxeter and S.L. Greitzer for more in - depth explanations of frustum - related formulas.
  • Python Documentation:
  • Unit Testing in Python: