0
0
PythonProgramBeginner · 2 min read

Python Program to Find Cube Root of a Number

You can find the cube root of a number in Python using the expression number ** (1/3) or by using pow(number, 1/3).
📋

Examples

Input27
Output3.0
Input64
Output4.0
Input-8
Output-2.0
🧠

How to Think About It

To find the cube root of a number, think about what number multiplied by itself three times equals the original number. We can use the power operator ** with the fraction 1/3 to calculate this directly in Python.
📐

Algorithm

1
Get the input number from the user or as a variable.
2
Calculate the cube root by raising the number to the power of 1/3.
3
Return or print the result.
💻

Code

python
number = float(input("Enter a number: "))
if number >= 0:
    cube_root = number ** (1/3)
else:
    cube_root = -(-number) ** (1/3)
print(f"Cube root of {number} is {cube_root}")
Output
Enter a number: 27 Cube root of 27.0 is 3.0
🔍

Dry Run

Let's trace the input 27 through the code

1

Input number

User enters 27, so number = 27.0

2

Calculate cube root

cube_root = 27.0 ** (1/3) = 3.0

3

Print result

Output: Cube root of 27.0 is 3.0

StepVariableValue
1number27.0
2cube_root3.0
3outputCube root of 27.0 is 3.0
💡

Why This Works

Step 1: Using power operator

The cube root of a number is the same as raising it to the power of 1/3, so number ** (1/3) calculates this directly.

Step 2: Handling negative numbers

Raising a negative number to the power 1/3 returns a complex number in Python, so for negative inputs, we adjust the calculation to keep the result real.

Step 3: Printing the result

We use print with an f-string to show the original number and its cube root clearly.

🔄

Alternative Approaches

Using pow() function
python
number = float(input("Enter a number: "))
if number >= 0:
    cube_root = pow(number, 1/3)
else:
    cube_root = -pow(-number, 1/3)
print(f"Cube root of {number} is {cube_root}")
This is similar to the power operator but uses a built-in function; readability is a matter of preference.
Handling negative numbers explicitly
python
number = float(input("Enter a number: "))
if number >= 0:
    cube_root = number ** (1/3)
else:
    cube_root = -(-number) ** (1/3)
print(f"Cube root of {number} is {cube_root}")
This approach ensures the cube root of negative numbers is a real number, avoiding complex results.

Complexity: O(1) time, O(1) space

Time Complexity

Calculating the cube root using the power operator or pow() is a constant time operation, so it is O(1).

Space Complexity

The calculation uses a fixed amount of memory for variables, so space complexity is O(1).

Which Approach is Fastest?

Using the power operator ** or pow() are equally fast; handling negatives explicitly adds minimal overhead but improves correctness.

ApproachTimeSpaceBest For
Power operator **O(1)O(1)Simple positive numbers
pow() functionO(1)O(1)Equivalent to power operator, preference-based
Explicit negative handlingO(1)O(1)Correct cube roots for negative inputs
💡
Use number ** (1/3) for a quick cube root calculation but handle negatives carefully.
⚠️
Beginners often forget that raising negative numbers to fractional powers can produce complex results in Python.