Advertisement
mhyusuf

Factorial Recursive

Oct 16th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.26 KB | None | 0 0
  1. function factorial($n){
  2.     return ($n == 1 || $n == 0) ? 1 : factorial($n - 1) * $n;
  3. }
  4.  
  5. // Test
  6.  
  7. //2! = 2.1 = 2
  8. echo factorial(2);
  9. //3! = 3.2.1 = 6
  10. echo factorial(3);
  11. //4! = 4.3.2.1 = 24
  12. echo factorial(4);
  13. //5! = 5.4.3.2.1 = 120
  14. echo factorial(5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement