php设计求1+2!+3!+4!+5!+6!的程序并上机调试输出运行结果
时间: 2023-09-20 14:10:22 浏览: 245
一个计算1+2+3+5+8+13+...+n的算法程序
3星 · 编辑精心推荐
以下是PHP代码实现1!到6!的计算,并输出结果:
```php
<?php
function factorial($n){
if($n == 0){
return 1;
}else{
return $n * factorial($n-1);
}
}
echo "1! = ".factorial(1)."\n";
echo "2! = ".factorial(2)."\n";
echo "3! = ".factorial(3)."\n";
echo "4! = ".factorial(4)."\n";
echo "5! = ".factorial(5)."\n";
echo "6! = ".factorial(6)."\n";
?>
```
运行结果:
```
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
```
阅读全文