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. It is also called a rectangular prism. A cuboid has six rectangular faces with twelve edges. The length, breadth, and height of a cuboid are different, but in the case of a cube, all sides are equal. Two examples are illustrated in this article. The first example uses the concept of a user-defined function and the second example uses the parameterized constructor of a defined class to calculate the cuboid’s volume, surface area, and its space diagonal.

Example 1:By utilizing User-defined function. (Coding Style 1)

Code Explanation and Design Steps

Step 1: Open Jupyter Notebook in the Anaconda prompt and start writing the code in its cell.

Step 2: Use the three functions: ‘cal_volume’, ‘cal_surface_area’, ‘cal_spac_diagonal’.

Step 3: Functions take the length, breadth, and height of the cuboid as input parameters, representing the dimensions of the cuboid.

Step 4: Use the formula for volume: Length (l) * Breadth (b) * Height (h).

Step 5: Use the formula for surface area: 2*( Length (l) * Breadth (b)+ Breadth (b) * Height (h) + Height (h) * Length (l).

Step 6: Calculate the surface area by multiplying every side of the cuboid by two and finally summing up the areas of all the sides. It will return the calculated values of the surface area.

Step 7: Use the formula for space diagonal: Space Diagonal = [( Length (l)2 *Breadth (b)2 * Length (l)2] 1/2. This is used to calculate the space diagonal of the cuboid using the Pythagoras theorem.

Step 8: Return the calculated values such as volume, surface area, and space diagonal.

Step 9: Check the result.

Example

import math
#User-defined function to calculate the volume
def calculate_volume(length, breadth, height):
    volume = length * breadth * height
    return volume
#Evaluate the Surface area
def calculate_surface_area(length, breadth, height):
    surface_area = 2 * (length * breadth + breadth * height + height * length)
    return surface_area
#Evaluate space diagonal of a cuboid
def calculate_space_diagonal(length, breadth, height):
    space_diagonal = math.sqrt(length ** 2 + breadth ** 2 + height ** 2)
    return space_diagonal

# Example 
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("Volume cuboid:", volume)
print("Surface Area cuboid:", surface_area)
print("Space Diagonal cuboid:", space_diagonal)

Output

Volume cuboid: 240
Surface Area cuboid: 236
Space Diagonal cuboid: 11.180339887498949

Example 2: By using the constructor.

Code Explanation and Design Steps

Step 1: Open Jupyter Notebook in the Anaconda prompt and start writing the code in its cell.

Step 2: Use a class named ‘Cuboid’.

Step 3: ‘def __init__(self, length, width, height)’: This is the constructor method of the ‘Cuboid’ class.

Step 4: The ‘init’ method is used to initialize the attributes of a ‘Cuboid’ object when it is created.

Step 5: ‘self’ refers to the instance of the class.

Step 6: The ‘init’ method takes four parameters: ‘self’, ‘length’, ‘breadth’, and ‘height’.

Step 7: Use the three functions: ‘cal_volume’, ‘cal_surface_area’, ‘cal_spac_diagonal’.

Step 8: Functions take the length, breadth, and height of the cuboid as input parameters, representing the dimensions of the cuboid.

Step 9: Use the formula for volume: Length (l) * Breadth (b) * Height (h)

Step 10: Use the formula for surface area: 2*( Length (l) * Breadth (b)+ Breadth (b) * Height (h) + Height (h) * Length (l). It calculates the surface area by multiplying every side of the cuboid by two and finally, summing up the areas of all the sides. It will return the calculated values of the surface area.

Step 11: The space diagonal is used to calculate the space diagonal of the cuboid using Pythagoras’ theorem.

Step 12: Return the calculated values such as volume, surface area, and space diagonal.

Step 13: Check the result.

Example

import math
#define class
class Cuboid:
    def __init__(self, length, width, height):
        self.length = length
        self.width = width
        self.height = height
   
    def cal_volume(self):
        return self.length * self.width * self.height
   
    def cal_surface_area(self):
        return 2 * (self.length * self.width + self.width * self.height + self.height * self.length)
   
    def cal_space_diagonal(self):
        return math.sqrt(self.length ** 2 + self.width ** 2 + self.height ** 2)

# Example
length = 11
width = 4
height = 7

cuboid = Cuboid(length, width, height)

volume = cuboid.cal_volume()
surface_area = cuboid.cal_surface_area()
space_diagonal = cuboid.cal_space_diagonal()

print("Volume cuboid:", volume)
print("Surface Area cuboid:", surface_area)
print("Space Diagonal cuboid:", space_diagonal)

Output

Volume cuboid: 308
Surface Area cuboid: 298
Space Diagonal cuboid: 13.638181696985855

In coding style 2, we can be defined a ‘Cuboid’ class to encapsulate the length, breadth, and height of the cuboid. Based on the cuboid’s attributes, we obtain results using these methods of the class.

In this article, using different functions in the example, we have shown how to find the volume, surface area, and space diagonal of the cuboid is shown. It can be useful in various scenarios, such as architecture and construction, packaging and logistics, 3D modeling and computer graphics, mathematics and education, and scientific research.

Updated on: 28-Aug-2023

919 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements