
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
How to find cube of a number?
The formula for finding the cube of a given number N is:
Cube = N Ã N Ã N or N3
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 5 is: 3 Ã 3 Ã 3 = 27
Different Approaches to Find the Cube of a Number
Using Direct Formula Approach
This is the simple and direct approach for finding the cube of a given number. In this approach, we use the direct method where we multiply the number by itself three times, which is represented by N3.
Steps for Implementation
- Take the input number.
- Calculate the cube using the formula: N Ã N Ã N.
- Output the result.
Implementation Code
# 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}")
Output:
The cube of the number 5 is: 125
Time Complexity: O(1)
Space Complexity: O(1)
Using the Exponentiation Operator
In this approach, we use the exponential operator (**) of Python to raise a number to a power. To find the cube of a number, we can raise the number to the power of 3 using the exponential operator.Steps for Implementation
- Take the input number.
- Calculate the cube using the exponentiation operator N3.
- Output the result.
Implementation Code
# 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}")
Output:
The cube of the number 3 is: 27
Time Complexity: O(1)
Space Complexity: O(1)
Using the math.pow() Function
The math.pow() is an inbuilt function of Python's math module. This method can be used to calculate the power of numbers. To find the cube of a number, we use math.pow( n , 3).
Steps for Implementation
- First, import the math module.
- Now, use math.pow(n, 3) to calculate the cube.
- Output the result.
Implementation Code
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}")
Output
The cube of the number 3 is: 27.0
Time Complexity: O(1)
Space Complexity: O(1)
Using a Function
In this approach, we define a function that accepts a number as an argument and returns the cube of the number. This approach is reusable and allows us to calculate the cube of any number by calling the function multiple times.
Steps for Implementation
- Define a function that accepts a number and calculates its cube.
- Call the function with the required input.
- Output the result.
Implementation Code
# Function to calculate the cube def calculate_cube(n): return n ** 3 # Using the exponentiation operator inside the function # Call the function n = 3 cube = calculate_cube(n) # Output the result print(f"The cube of the number {n} is: {cube}")
Output
The cube of the number 3 is: 27
Time Complexity: O(1)
Space Complexity: O(1)
Using a Loop (Multiplication)
This is a simple and iterative approach. In this approach, we use a loop to multiply the number by itself three times. This is not the most efficient method, but it is used to understand the iterative process.Steps for Implementation
- Initialize a variable to hold the result.
- Multiply the number by itself three times inside a loop.
- Output the result
Implementation Code
# 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}")
Output:
The cube of the number 3 is: 27
Time Complexity: O(1)
Space Complexity: O(1)