Q:

PHP program to calculate the factorial of a given number using recursion

0

In this program, we will calculate the factorial of the specified given number. The factorial of 5 is 120.
5! = 5*4*3*2*1=120

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to calculate the factorial of a given number using recursion is given below. The given program is compiled and executed successfully.

<?php
//PHP program to calculate the factorial 
//of a given number using recursion.
function fact($num)
{
    if ($num == 1) return 1;

    return $num * fact($num - 1);
}

$result = fact(5);
echo "Factorial is: " . $result;
?>

Output:

Factorial is: 120

Explanation:

In the above program, we created a recursive function fact() to calculate the factorial of a given number, The fact() function returns the factorial of the number to the calling function, in our program, we calculated the factorial of 5 that is 120.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now