/* Computing factorial of n recursively. Laura Toma csci101 */ public class Fact { public static void main (String args[]) { int k = 10; //compute the product of the first k numbers int s = fact(k); System.out.println("fact( " + k + ") is " + s); } // end of main //a recursive method to compute the sum 1 + 2 + .... + k public static int fact (int k) { if (k > 0) return k * fact(k-1); else return 1; } //for comparison, here is an iterative version of fact public static int factIterative(int k) { int i, f = 1; for (i = 0; i< k; i++) f = f * i; return f; } } // end of class