Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static int fact(int n) {
- if (n == 0 || n == 1) {
- return 1;
- }
- return n * fact(n - 1);
- }
- public static void main(String[] args) {
- int result = fact(5);
- System.out.println(result);
- }
- }
- /*
- fact(5) = 5 * fact(4) = 120
- fact(4) = 4 * fact(3) = 4 * 6 = 24
- fact(3) = 3 * fact(2) = 3 * 2 = 6
- fact(2) = 2 * fact(1) = 2 * 1 = 2
- fact(1) = 1
- */
Add Comment
Please, Sign In to add comment