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 calculate the volume of Cube
A cube is a three-dimensional solid figure with six faces, twelve edges and eight vertices. This geometrical figure has equal sized edges, hence making all its dimensions ? length, width and height ? equal.
The idea of calculating the volume of a cube can be understood in a simple way. Consider a real-time situation where a person is moving houses. Suppose they use a hollow cube shaped cardboard box to place their things in it, the amount of space present to fill it up is defined as the volume.
The mathematical formula to calculate the volume of a cube with a side length 'a' is as follows ?
Volume = a³ = a × a × a
Input Output Scenarios
The python program to calculate the volume of the cube will provide the following input-output scenarios ?
Assume the length of the cube edge is positive, the output is obtained as ?
Input: 6 // 6 is the edge length Result: Volume of the cube = 216
Assume the length of the cube edge is negative, the output is obtained as ?
Input: -6 // -6 is the edge length Result: Not a valid length
Using Mathematical Formula
As seen above, the formula to calculate the volume of a cube is relatively simple. Therefore, we write a python program to display the volume by taking the length of an edge in a cube as an input ?
Example
In the python code below, we calculate the cube volume using its mathematical formula and the length of the cube is taken as the input ?
# Length of the cube edge
cube_edge = 5
# Calculating the volume
cube_volume = cube_edge * cube_edge * cube_edge
# Displaying the volume
print("Volume of the cube:", cube_volume)
Output
The volume of a cube is displayed as the output for the python code above ?
Volume of the cube: 125
Using a Function to Calculate Volume
Another way to calculate volume of a cube in python is using functions. Python has various built-in functions but also has a provision to declare user-defined functions. We use def keyword to declare functions and can pass as many arguments as we want ?
The syntax to declare a function is ?
def function_name(arguments separated using commas):
# function body
return value
Example
In the following python program, we create a function to calculate and display the cube volume ?
# Function to calculate volume of a cube
def calculate_volume(cube_edge):
# Calculating the volume
cube_volume = cube_edge * cube_edge * cube_edge
# Displaying the volume
print("Volume of the cube:", cube_volume)
return cube_volume
# Calling the volume function
calculate_volume(5) # length of an edge = 5
Output
On executing the python code above, the output is displayed as ?
Volume of the cube: 125
Using Input Validation
We can improve our program by adding input validation to handle negative values ?
def calculate_cube_volume(edge_length):
# Check if edge length is valid
if edge_length <= 0:
print("Not a valid length")
return None
# Calculate volume
volume = edge_length ** 3 # Using exponent operator
print(f"Volume of the cube = {volume}")
return volume
# Test with positive value
calculate_cube_volume(6)
# Test with negative value
calculate_cube_volume(-6)
Output
Volume of the cube = 216 Not a valid length
Conclusion
Calculating the volume of a cube is straightforward using the formula a³. Python provides multiple approaches including direct calculation, functions, and input validation to create robust cube volume calculators.
