Home > Backend Development > PHP Tutorial > PHP implements factorial operation small code

PHP implements factorial operation small code

WBOY
Release: 2016-07-23 08:54:56
Original
1359 people have browsed it
  1. $sum = 1;
  2. $num = 7;
  3. if( $num>0 ){
  4. for($i=$num;$i>0;$i--){
  5. $sum*=$i;
  6. }
  7. echo $sum;
  8. }elseif( $num<0 ){
  9. //Note here is ++, negative numbers closer to 0 are ++
  10. for($i=$num; $i<0;$i++){
  11. $sum*=$i;
  12. }
  13. echo $sum;
  14. }else{
  15. echo "0";
  16. }
  17. ?>
Copy code

PHP


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