Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {

Similar presentations


Presentation on theme: "Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {"— Presentation transcript:

1 Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial { public static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } public static void main(String[] args) { System.out.println(fact(3)); }

2 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment

3 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment

4 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment

5 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment

6 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment

7 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment

8 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(1) n = 1 environment

9 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(1) n = 1 environment

10 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(1) n = 1 environment

11 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(1) n = 1 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(0) n = 0 environment

12 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(1) n = 1 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(0) n = 0 environment

13 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(1) n = 1 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(0) n = 0 environment 1

14 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(1) n = 1 environment 11

15 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(1) n = 1 environment 11 1

16 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment 1 2

17 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(2) n = 2 environment 1 2 2

18 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment 23

19 static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } fact(3) n = 3 environment 23 % java Factorial 6 public class Factorial { public static int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } public static void main(String[] args) { System.out.println(fact(3)); } 6


Download ppt "Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {"

Similar presentations


Ads by Google