Advertisement
Mouamle

Factorial

Sep 13th, 2016
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.39 KB | None | 0 0
  1. /**
  2.     * @param n - max is 20
  3.     * @return The factorial of the number
  4. */
  5. public static long stackFactorial(int n) {
  6.     if (n == 1 || n == 0)
  7.         return 1;
  8.     else
  9.         return stackFactorial(n - 1) * n;
  10. }
  11.  
  12. /**
  13.     * @param n - max is 20
  14.     * @return The factorial of the number
  15. */
  16. public static long loopFactorial(int n) {
  17.     long r = 1;
  18.     for (long i = 1; i <= n; i++) {
  19.         r *= i;
  20.     }
  21.     return r;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement