// Source code example for "A Practical Introduction // to Data Structures and Algorithm Analysis" // by Clifford A. Shaffer, Prentice Hall, 1998. // Copyright 1998 by Clifford A. Shaffer import java.io.*; // Driver class for testing factorial function class Factmain { // Compute factorial static long fact(int n) { // Must have n < 21 to fit in long Assert.notFalse((n >= 0) && (n < 21), "Input out of range"); Stack S = new AStack(n-1); // Make stack just big enough while (n > 1) S.push(new Integer(n--)); long result = 1; while (!S.isEmpty()) result = result * ((Integer)S.pop()).longValue(); return result; } // Main routine for factorial driver class public static void main(String args[]) throws IOException { for(int i = 1; i<25; i++) System.out.println("Factorial of " + i + " is " + fact(i)); System.in.read(); } } // class Rfactmain