php

Factorial in PHP Using For Loop

In this tutorial, we are going to see how to write a PHP function to calculate the factorial of a given number in PHP using for loop. The factorial of a number is the product of all integers between 1 and itself.
 

Factorial in PHP Using For Loop
<?php
	function fact($n){ 
		$f = 1; 
		for ($i = 1; $i <= $n; $i++){ 
		  $f = $f * $i; 
		} 
		return $f; 
	} 
	  
	$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

Leave a Reply

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