PHP Program to Calculate the Power of a Number using For loop
ADVERTISEMENTS
PHP program to calculate the power of a number using for loop. In this article, you will learn how to calculate the power of a numbers in php.
Syntax to calculate the power of N number
x ^ y = results
PHP Program to Calculate the Power of a Number using For loop
<?php
// PHP Program to Calculate the Power of a Number using For loop
$b = 8;
$x = 5;
$r = 1;
// b - To store base number
// x - To store exponent number
// r - To store result value
for ($i = $x; $i > 0; $i--) {
$r *= $b;
}
echo "The calculation of the power of N number is {$b}^{$x} = {$r}";
?>
Output
The calculation of the power of N number is 8^5 = 32768
PHP Program to Calculate the Power of a Number using pow() Function
<?php
// PHP Program to Calculate the Power of a Number using pow() Function
$b = 8;
$x = 3;
$r = 1;
// b - To store base number
// x - To store exponent number
// r - To store result value
$r = pow($b, $x);
echo "The calculation of the power of N number is {$b}^{$x} = {$r}";
?>
Output
The calculation of the power of N number is 8^3 = 512