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, surface area and space diagonal of a cuboid
In this article, we will discuss how to compute the volume, surface area, and space diagonal of a cuboid. A cuboid is a 3D geometric shape that resembles a rectangular box, also called a rectangular prism. It has six rectangular faces with twelve edges, where length, breadth, and height are different (unlike a cube where all sides are equal).
Understanding Cuboid Formulas
Before implementing the code, let's understand the key formulas ?
- Volume: length × breadth × height
- Surface Area: 2 × (l×b + b×h + h×l)
- Space Diagonal: ?(l² + b² + h²)
Method 1: Using Functions
This approach uses separate functions for each calculation ?
import math
def calculate_volume(length, breadth, height):
"""Calculate the volume of a cuboid"""
return length * breadth * height
def calculate_surface_area(length, breadth, height):
"""Calculate the surface area of a cuboid"""
return 2 * (length * breadth + breadth * height + height * length)
def calculate_space_diagonal(length, breadth, height):
"""Calculate the space diagonal of a cuboid using Pythagorean theorem"""
return math.sqrt(length ** 2 + breadth ** 2 + height ** 2)
# Example with dimensions
length = 8
breadth = 5
height = 6
volume = calculate_volume(length, breadth, height)
surface_area = calculate_surface_area(length, breadth, height)
space_diagonal = calculate_space_diagonal(length, breadth, height)
print(f"Cuboid dimensions: {length} × {breadth} × {height}")
print(f"Volume: {volume}")
print(f"Surface Area: {surface_area}")
print(f"Space Diagonal: {space_diagonal:.2f}")
Cuboid dimensions: 8 × 5 × 6 Volume: 240 Surface Area: 236 Space Diagonal: 11.18
Method 2: Using Object-Oriented Approach
This method encapsulates all calculations within a Cuboid class ?
import math
class Cuboid:
def __init__(self, length, width, height):
"""Initialize cuboid with dimensions"""
self.length = length
self.width = width
self.height = height
def calculate_volume(self):
"""Return the volume of the cuboid"""
return self.length * self.width * self.height
def calculate_surface_area(self):
"""Return the surface area of the cuboid"""
return 2 * (self.length * self.width + self.width * self.height + self.height * self.length)
def calculate_space_diagonal(self):
"""Return the space diagonal of the cuboid"""
return math.sqrt(self.length ** 2 + self.width ** 2 + self.height ** 2)
def get_all_measurements(self):
"""Return all measurements as a dictionary"""
return {
'volume': self.calculate_volume(),
'surface_area': self.calculate_surface_area(),
'space_diagonal': self.calculate_space_diagonal()
}
# Example usage
cuboid = Cuboid(11, 4, 7)
print(f"Cuboid dimensions: {cuboid.length} × {cuboid.width} × {cuboid.height}")
print(f"Volume: {cuboid.calculate_volume()}")
print(f"Surface Area: {cuboid.calculate_surface_area()}")
print(f"Space Diagonal: {cuboid.calculate_space_diagonal():.2f}")
Cuboid dimensions: 11 × 4 × 7 Volume: 308 Surface Area: 298 Space Diagonal: 13.64
Comparison
| Approach | Best For | Advantages |
|---|---|---|
| Functions | Simple calculations | Direct, easy to understand |
| Class-based | Multiple cuboids | Reusable, organized, extensible |
Practical Applications
These calculations are useful in various scenarios ?
- Architecture: Calculating room volumes and wall areas
- Packaging: Determining box capacities and material requirements
- 3D Modeling: Computing object properties in graphics applications
- Education: Teaching geometry and spatial reasoning
Conclusion
Both approaches effectively calculate cuboid properties using standard geometric formulas. Use functions for simple one-time calculations, and classes when working with multiple cuboids or when you need organized, reusable code.
