19
Introduction to Computer Science Robert Sedgewick and Kevin Wayne http://www.cs.Princeton.EDU/IntroCS 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)); } }

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

Embed Size (px)

Citation preview

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

Introduction to Computer Science • Robert Sedgewick and Kevin Wayne • http://www.cs.Princeton.EDU/IntroCS

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)); }}

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

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

fact(3)n = 3

environment

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

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

fact(3)n = 3

environment

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

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

fact(3)n = 3

environment

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

12

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

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

12

2

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

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

fact(3)n = 3

environment

23

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

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

fact(3)n = 3

environment

23

% java Factorial6

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