Advertisement
mhyusuf

Factorial

Oct 16th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.44 KB | None | 0 0
  1. <?php
  2. // https://blog.ruangguru.com/aturan-perkalian-dan-faktorial-dalam-teori-peluang
  3.  
  4. function factorial($n) {
  5.     echo "$n! = ";
  6.     $f = 1;
  7.     for($i=1; $i<=$n; $i++) {
  8.         $f = $f * $i;
  9.         echo "$i" . ( ($i<$n)? "." : "" );
  10.     }
  11.    
  12.     echo "= $f <br/>";
  13. }
  14.  
  15. // Test
  16.  
  17. //2! = 2.1 = 2
  18. factorial(2);
  19. //3! = 3.2.1 = 6
  20. factorial(3);
  21. //4! = 4.3.2.1 = 24
  22. factorial(4);
  23. //5! = 5.4.3.2.1 = 120
  24. factorial(5);
  25.  
  26. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement