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 Cube of a Number
The cube of a number is a simple mathematical operation where a number is multiplied by itself three times. In Python, there are multiple ways to find the cube of a number. In this article, we are going to discuss various approaches to calculating the cube of a number in Python.
What is the Cube of a Number?
The formula for finding the cube of a given number N is:
Cube = N × N × N or N³
Example 1
- Input: 5
- Output: 125
Explanation:
The cube of 5 is: 5 × 5 × 5 = 125
Example 2
- Input: 3
- Output: 27
Explanation:
The cube of 3 is: 3 × 3 × 3 = 27
Using Direct Multiplication
This is the simple and direct approach for finding the cube of a given number. In this approach, we multiply the number by itself three times ?
# Define the number
n = 5
# Calculate the cube using the formula
cube = n * n * n
# Output the result
print(f"The cube of the number {n} is: {cube}")
The cube of the number 5 is: 125
Using the Exponentiation Operator
In this approach, we use the exponential operator (**) of Python to raise a number to the power of 3 ?
# Define the number
n = 3
# Calculate the cube using exponentiation
cube = n ** 3
# Output the result
print(f"The cube of the number {n} is: {cube}")
The cube of the number 3 is: 27
Using the math.pow() Function
The math.pow() function from Python's math module can be used to calculate the power of numbers. To find the cube of a number, we use math.pow(n, 3) ?
import math
# Define the number
n = 3
# Calculate the cube using math.pow()
cube = math.pow(n, 3)
# Output the result
print(f"The cube of the number {n} is: {cube}")
The cube of the number 3 is: 27.0
Note: math.pow() returns a float value, so the result is 27.0 instead of 27.
Using a Function
We can define a function that accepts a number as an argument and returns its cube. This approach is reusable and allows us to calculate the cube of any number ?
# Function to calculate the cube
def calculate_cube(n):
return n ** 3
# Call the function
n = 4
cube = calculate_cube(n)
# Output the result
print(f"The cube of the number {n} is: {cube}")
# Test with different numbers
numbers = [2, 3, 5, 7]
for num in numbers:
print(f"Cube of {num} is: {calculate_cube(num)}")
The cube of the number 4 is: 64 Cube of 2 is: 8 Cube of 3 is: 27 Cube of 5 is: 125 Cube of 7 is: 343
Using a Loop (Multiplication)
This is an iterative approach where we use a loop to multiply the number by itself three times. While not the most efficient method, it demonstrates the iterative process ?
# Define the number
n = 3
# Initialize result variable
result = 1
# Loop to multiply the number by itself 3 times
for _ in range(3):
result *= n
# Output the result
print(f"The cube of the number {n} is: {result}")
The cube of the number 3 is: 27
Comparison of Methods
| Method | Syntax | Return Type | Best For |
|---|---|---|---|
| Direct Multiplication | n * n * n | Same as input | Simple calculations |
| Exponentiation | n ** 3 | Same as input | Most readable |
| math.pow() | math.pow(n, 3) | Float | Complex calculations |
| Function | calculate_cube(n) | Same as input | Reusability |
| Loop | for loop | Same as input | Learning purposes |
Conclusion
Python provides multiple ways to calculate the cube of a number. The exponentiation operator (**) is the most readable and efficient approach for simple calculations. Use functions when you need reusability across your program.
