Ngôn ngữ PHP - Để tính giai thừa của một số dương

Viết hàm tính giai thừa của một số (số nguyên không âm)

Ví dụ:

PHP function Exercises: Create a function to calculate the factorial of positive a number

Code PHP:

<?php
function factorial_of_a_number($n)
{
  if($n ==0)
    {
	   return 1;
    }
  else 
    {	
	   return $n * factorial_of_a_number($n-1);
    }
	}
print_r(factorial_of_a_number(4)."\n");
?>

Xem ví dụ

Lưu đồ thuật toán:

Flowchart: Create a function to calculate the factorial of positive a number