Home  • Programming • PHP

Problem: Find the factorial in PHP

Method 1: By Loop

function factorial($n){
  $factorial = 1;
  for ($i=$n; $i>=1; $i--)
  {
   $factorial = $factorial * $i;
   }
 return $factorial;
}

Method 2: By Recursive

function factorial($n){
    if (1<$n){ 
	return $n*factorial($n-1);        
    }else { 
        return 1; 
    }
}
$result=factorial(6); echo $result;

Comments 1


Copyright © 2026. Powered by Intellect Software Ltd