R Examples

R Program - Cube Root of a Number



If a number is multiplied by itself two times (n*n*n), the final number will be the cube of that number and finding the cube root of a number is inverse operation of cubing the number. If x is the cube root of y, it can be expressed as below:

cube root

Alternatively, it can also be expressed as:

x3 = y

Method 1: Using exponential (**) operator

The exponential (**) operator of R can be used to calculate the cube root of a number. Consider the following example.

x <- 64
y <- 125
x1 <- x**(1/3)
y1 <- y**(1/3)
sprintf("Cube root of %f is %f", x, x1)
sprintf("Cube root of %f is %f", y, y1)

The above code will give the following output:

[1] "Cube root of 64.000000 is 4.000000"
[1] "Cube root of 125.000000 is 5.000000"

Method 2: Using exponential (^) operator

Alternatively, the caret symbol (^) can also be used as exponential/power operator to calculate the cube root of a number. Consider the following example.

x <- 64
y <- 125
x1 <- x^(1/3)
y1 <- y^(1/3)
sprintf("Cube root of %f is %f", x, x1)
sprintf("Cube root of %f is %f", y, y1)

The above code will give the following output:

[1] "Cube root of 64.000000 is 4.000000"
[1] "Cube root of 125.000000 is 5.000000"