php

Factorial Program in PHP Using Recursive Function

In this tutorial, we are going to see how to write a factorial program in PHP using recursive function. Factorial of a number is the product of all integers between 1 and itself. In programming, a recursive function is a function that calls itself when it is executed. This allows the function to repeat itself multiple times, producing the result at the end of each iteration. Here is an example of a recursive function.
 

Factorial Program in PHP Using Recursive Function
<?php
	function fact($n){
		if($n <= 1){
			return 1;   
		}
		else{
			return $n * fact($n - 1);
		}
	}
	  
	$n = 5; 
	$f = fact($n); 
	echo "Factorial of $n is $f"; 
?>

Output:

Factorial of 5 is 120
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More [st_adsense]

Leave a Reply

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