190 likes | 296 Views
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 )); } }. static int fact ( int n ) {
E N D
Recursive Factorial Demo pubic class Factorial { public staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } publicstaticvoid main(String[] args) { System.out.println(fact(3)); } }
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) n = 3 environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) n = 3 environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) n = 3 environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) n = 3 n = 2 environment environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) n = 3 n = 2 environment environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) n = 3 n = 2 environment environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(1) n = 3 n = 2 n = 1 environment environment environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(1) n = 3 n = 2 n = 1 environment environment environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(1) n = 3 n = 2 n = 1 environment environment environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(1) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(0) n = 3 n = 2 n = 1 n = 0 environment environment environment environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(1) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(0) n = 3 n = 2 n = 1 n = 0 environment environment environment environment
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(0) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(1) fact(2) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } n = 0 n = 3 n = 2 n = 1 environment environment environment environment 1
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(1) n = 3 n = 2 n = 1 environment environment environment 1 1
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(1) n = 3 n = 2 n = 1 environment environment environment 1 1 1
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) n = 3 n = 2 environment environment 1 2
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(2) n = 3 n = 2 environment environment 2 1 2
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) n = 3 environment 3 2
staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } fact(3) n = 3 environment 3 2 public class Factorial { public staticint fact(int n) { if(n ==0)return1; elsereturn n * fact(n-1); } publicstaticvoid main(String[] args) { System.out.println(fact(3)); } } % java Factorial 6 6