Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
hackerss
hackerss

Posted on

Php Calculate factorial of a number using recursion


function factorial($n) {
  // base case
  if ($n == 0) {
    return 1;
  }
  // recursive case
  return $n * factorial($n - 1);
}

//example
$result = factorial(5);

//output
echo $result; //120
Enter fullscreen mode Exit fullscreen mode

Top comments (0)