Wed May 27 2020

Recursive Function

PHP Scripting942 views

File Name: recursive-function.php

<!DOCTYPE html>
<html>
	<head>
		<title>Program for Recursive Factorial</title>
	</head>
	<body>
		<form method="post" action="">
			<input type="text" name="data" placeholder="Enter a Number to calculate Factorial" />
			<input type="submit" value="Calcualte" />
		</form>
		<br />
		<?php
			if(isset($_POST['data'])) {
				
				/* Recursive function */
				function factorial($no) {
					if($no == 1)
						return 1;
					else
						/* Call the factorial function inside in it */
						return($no * factorial($no - 1));
				}
	
				/* Call user define function with parameter */
				echo "Factorial value is: ".factorial($_POST['data']);
			}
		?>
	</body>
</html>



/* Output
Input:
5

Factorial value is: 120
*/

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.