Guest User

Untitled

a guest
Mar 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.37 KB | None | 0 0
  1. public class Test {
  2. public static int fact(int n) {
  3. if (n == 0 || n == 1) {
  4. return 1;
  5. }
  6. return n * fact(n - 1);
  7. }
  8.  
  9. public static void main(String[] args) {
  10. int result = fact(5);
  11. System.out.println(result);
  12. }
  13. }
  14.  
  15. /*
  16. fact(5) = 5 * fact(4) = 120
  17. fact(4) = 4 * fact(3) = 4 * 6 = 24
  18. fact(3) = 3 * fact(2) = 3 * 2 = 6
  19. fact(2) = 2 * fact(1) = 2 * 1 = 2
  20. fact(1) = 1
  21. */
Add Comment
Please, Sign In to add comment