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:
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.
Recommended Pages
- PHP Program - To Check Prime Number
- PHP Program - Bubble Sort
- PHP Program - Selection Sort
- PHP Program - Maximum Subarray Sum
- PHP Program - Reverse digits of a given Integer
- PHP - Swap two numbers
- PHP Program - Fibonacci Sequence
- PHP Program - Insertion Sort
- PHP Program - Find Factorial of a Number
- PHP Program - Find HCF of Two Numbers
- PHP Program - To Check Whether a Number is Palindrome or Not
- PHP Program - To Check Whether a String is Palindrome or Not
- PHP Program - Heap Sort
- PHP Program - Quick Sort
- PHP - Swap Two Numbers without using Temporary Variable
- PHP Program - To Check Armstrong Number
- PHP Program - Counting Sort
- PHP Program - Radix Sort
- PHP Program - Find Largest Number among Three Numbers
- PHP Program - Print Floyd's Triangle