PHP Examples

PHP 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 exponent operator of PHP

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

<?php
$x = 64;
$y = 125;

$x1 = $x**(1/3);
$y1 = $y**(1/3);
echo "Cube root of $x is $x1.\n";
echo "Cube root of $y is $y1.\n";
?>

The above code will give the following output:

Cube root of 64 is 4.
Cube root of 125 is 5.

Method 2: Using pow() function of PHP

The pow() function of PHP can also be used to calculate cube root of a number.

<?php
$x = 64;
$y = 125;

$x1 = pow($x, 1/3);
$y1 = pow($y, 1/3);
echo "Cube root of $x is $x1.\n";
echo "Cube root of $y is $y1.\n";
?>

The above code will give the following output:

Cube root of 64 is 4.
Cube root of 125 is 5.