How to Find Cube Root in Python

In the previous article, we have discussed Python Program for isfinite() Function
Given a number and the task is to calculate the cube root of a given number in Python.

Cube root:

A cube root is a number that, when cubed, yields a given number. A number’s cube root is a value that, when multiplied by itself three times, yields the original value.

‘3√’ represents the cube root symbol. In the case of square root, we simply used the root symbol, such as ‘√’, which is also known as a radical.

For example :

The cube root of 27 is 3. (3*3*3=27)

Cube root of a Negative Number:

We cannot find the cube root of negative numbers using the method described above. The cube root of integer -27, for example, should be -3, but Python returns 1.5000000000000004+2.598076211353316j.

To calculate the cube root of a negative number in Python, first, use the abs() function, and then use the simple math equation.

Examples:

Example1:

Input:

Given Number = 125

Output:

The cube root of the given number { 125 } =  4.999999999999999

Example2:

Input:

Given Number = -27

Output:

The cube root of the given number { -27 } = -3.0

Program for How To Find Cube Root in Python

Below are the ways to calculate the cube root of a given number in python:

Method #1: Using Power Operator (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Check if the given is less than 0 or not using the if conditional statement.
  • If it is true, then get the absolute value of the given number using the abs() function and store it in another variable.
  • Calculate the value of a given number raised to the power (1/3) and multiplied with -1 to get the cube root of the given number.
  • Store it in another variable say rslt_cuberoott.
  • Else, calculate the value of a given number raised to the power (1/3) to get the cube root of the given number.
  • Store it in the same variable say rslt_cuberoott.
  • Print the cube root of the given number
  • The Exit of Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 125
# Check if the given is less than 0 or not using the if conditional statement.
if gvn_numb < 0:
    # If it is true, then get the absolute value of the given number using the
    # abs() function and store it in the another variable.
    abs_numbr = abs(gvn_numb)
    # Calculate the value of the given number raised to the power (1/3) and
    # multiplied with -1 to get the cube root of the given number.
    # Store it in another variable say rslt_cuberoott.
    rslt_cuberoott = abs_numbr**(1/3)*(-1)
else:
    # Else, calculate the value of a given number raised to the power (1/3) to
    # get the cube root of the given number.
    # Store it in the same variable say rslt_cuberoott.
    rslt_cuberoott = gvn_numb**(1/3)
# Print the cube root of the given number
print("The cube root of the given number {", gvn_numb, "} = ", rslt_cuberoott)

Output:

The cube root of the given number { 125 } =  4.999999999999999

Method #2: Using Power Operator (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Check if the given is less than 0 or not using the if conditional statement.
  • If it is true, then get the absolute value of the given number using the abs() function and store it in another variable.
  • Calculate the value of a given number raised to the power (1/3) and multiplied with -1 to get the cube root of the given number.
  • Store it in another variable say rslt_cuberoott.
  • Else, calculate the value of a given number raised to the power (1/3) to get the cube root of the given number.
  • Store it in the same variable say rslt_cuberoott.
  • Print the cube root of the given number
  • The Exit of Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and 
# store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Check if the given is less than 0 or not using the if conditional statement.
if gvn_numb < 0:
    # If it is true, then get the absolute value of the given number using the
    # abs() function and store it in the another variable.
    abs_numbr = abs(gvn_numb)
    # Calculate the value of the given number raised to the power (1/3) and
    # multiplied with -1 to get the cube root of the given number.
    # Store it in another variable say rslt_cuberoott.
    rslt_cuberoott = abs_numbr**(1/3)*(-1)
else:
    # Else, calculate the value of a given number raised to the power (1/3) to
    # get the cube root of the given number.
    # Store it in the same variable say rslt_cuberoott.
    rslt_cuberoott = gvn_numb**(1/3)
# Print the cube root of the given number
print("The cube root of the given number {", gvn_numb, "} = ", rslt_cuberoott)

Output:

Enter some random number = -27
The cube root of the given number { -27 } = -3.0

Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.