Find Factorial of a number in PHP

Find Factorial of a Number using While Loop

<?php 


	function getFactorial( $num ){
		$fact = 1;
		while( $num != 0 ){
			$fact = $fact * $num;
			$num--;
		}
		return $fact;
	}

	$number = 5;
    $factorial = getFactorial( $number );

    echo 'Factorial of '.$number. ' is '.$factorial;


?>

Find Factorial of a Number using Recursion in PHP

<?php

  function factorial( $num )  
  {  
      if( $num <= 1 )   
      {  
          return 1;  
      }  
      else   
      {  
          return $num * factorial( $num - 1 );  
      }  
  }  
  
  $number = 6;  
  echo "Factorial of ".$number." is " .factorial( $number );  
 

?>

Leave a Comment