Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and work.

aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Linux Training
Teach Your Child To Read

BlueHost Web Hosting Black Friday

Bonsai Boy of New York Bonsai Trees and Accessories

DreamCloud The Comfortable Luxury Mattress Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

16,913 questions

22,602 answers

573 users

How to write a recursive function that calculate power in PHP

Linux Foundation - Training


2,062 views
asked Aug 1, 2014 by avibootz
edited Feb 26, 2016 by avibootz

1 Answer

0 votes
printf("%d\n<br />", power(2, 3)); // 8
printf("%d\n<br />", power(2, 4)); // 16
printf("%d\n<br />", power(3, 2)); // 9
printf("%d\n<br />", power(3, 3)); // 27
printf("%d\n<br />", power(5, 4)); // 625
          
echo "<br />";
 
echo power(2, 3) . "<br />"; // 8
echo power(2, 4) . "<br />"; // 16
echo power(3, 2) . "<br />"; // 9
echo power(3, 3) . "<br />"; // 27
echo power(5, 4) . "<br />"; // 625
 
function power($x, $y)
{
    if ($y == 0)
        return 1;
    return $x * power($x, $y - 1);
}

    
/*
run:

8
16
9
27
625

8
16
9
27
625
     
*/



answered Aug 1, 2014 by avibootz
edited Feb 26, 2016 by avibootz
...