Calculate factorial using recursion in php program

GOCOURSE

Factorial using recursion :

Program:

<?php
function factorial($n) {
    if ($n == 1) {
        return 1;
    } else {
        return $n * factorial($n - 1);
    }
}

$num = 5;
echo "The factorial of $num is: " . factorial($num);
?>

Output:

The factorial of 5 is: 120
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send