Recursion in php

Recursion :

What is recursion ?

Recursion means factorial . A recursive function is one that calls an instance of the same function from withing itself. There are times when this type of recursion can overcome problems and handle scenarios that would otherwise be imposiible .

 

syntex :

n! = n * ((n – 1)!)

 

Code :

example 1 :

<?php

function factorial($n)
{
if($n == 0 || $n == 1)
{
return 1;
}
else{
return ($n * factorial($n – 1));
}
}

echo factorial(15);

?>

 

example 2 :

<?php

function factoria($n)
{
$result = 1;
if($n == 0 || $n == 1)
{
return 1;
}
else
{
for($i = 1; $i <= $n; $i++)
$result *= $i;
return $result;
}
}

echo factoria(11);

?>

 

 

You have to learning web developer bootcamp full course at reasonable price send me message at

admin@studentsempire.com  . 

Leave a Reply

Your email address will not be published. Required fields are marked *