logo
logo
Sign in

How to get Factorial of a number using Recursive Function | PHP Gurukul

avatar
PHPGurukul

Factorial, in mathematics, the product of all positive integers less than or equal to a given positive integer and denoted by that integer and an exclamation point. Thus, factorial seven is written 7! meaning 1 × 2 × 3 × 4 × 5 × 6 × 7. Factorial zero is defined as equal to 1.

Click : https://phpgurukul.com/how-to-get-factorial-of-a-number-using-recursive-function/

In programming, a recursive function is a function that calls itself when it’s executed. This allows the function to repeat itself multiple times, producing the result at the end of each replication.

Here is the Example

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

 

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>factorial-recursive-function</title>

</head>

<body>

<h4>Factorial of a Given Number using Recursive function</h4>

<hr />

<!--- PHP Code -->

<?php

if(isset($_POST['submit'])){

function fact($n){

if($n <= 1){

return 1;

}

else{

return $n * fact($n - 1);

}

}

$n =$_POST['number']; // Input Number

$f = fact($n);

echo "Factorial of $n is $f";

}

?>

 

<form method="post">

<p>Enter the Number </p>

<input type="text" name="number" required>

<input type="submit" name="submit">

</form>

 

</body>

</html>

About PHP Gurukul

Welcome to PHPGurukul. We are a web development team striving our best to provide you with an unusual experience with PHP. Some technologies never fade, and PHP is one of them. From the time it has been introduced, the demand for PHP Projects and PHP developers is growing since 1994. We are here to make your PHP journey more exciting and useful.

Website : https://phpgurukul.com/

collect
0
avatar
PHPGurukul
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more