Home > Backend Development > PHP Tutorial > PHP method to find factorial of a number based on recursive function

PHP method to find factorial of a number based on recursive function

墨辰丷
Release: 2023-03-27 17:02:02
Original
3418 people have browsed it

This article mainly introduces the PHP method of finding the factorial of a number based on a simple recursive function. It analyzes the definition of the PHP recursive function and the simple operation skills of mathematical operations in the form of examples. Friends in need can refer to it

The example in this article describes how to find the factorial of a number in PHP based on a simple recursive function. Share it with everyone for your reference, the details are as follows:

1. Question:

Find the factorial of a number a, then, a!=a*(a-1 )*(a-2)*(a-3)*……*2*1. For example, the factorial of 6 is 6! =6*5*4*3*2*1=720. So, how to find the factorial of any number through PHP code?

2. Implementation code:

<?php
function demo($a) {
  if ($a > 1) {
    $r = $a * demo($a - 1);
  } else {
    $r = $a;
  }
  return $r;
}
$a = 6;
echo $a . "的阶乘的值" . demo($a);
?>
Copy after login

3. Display result:

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP’s global error handling details_php skills

PHPSimple method to generate txt files to a specified directory_php skills

PHPimplementation Detailed explanation of clues for binary trees and binary tree traversal methods_php skills

The above is the detailed content of PHP method to find factorial of a number based on recursive function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template