Output

Call Stack
factorial(0): n: 0
factorial(1): n: 1
factorial(2): n: 2
factorial(3): n: 3
main:
n: 3
Variable Name     Value in Memory
n
n
numberOfOneDollars
numberOfQuarters
numberOfDimes
numberOfNickels
numberOfPennies
  1  import java.util.Scanner; 
  2  
  3  public class ComputeFactorial {
  4    /** Main method */
  5    public static void main(String[] args) {
  6      // Create a Scanner
  7      Scanner input = new Scanner(System.in);
  8      System.out.print("Enter a non-negative integer: ");
  9      int n = input.nextInt();
 10      
 11      // Display factorial
 12      System.out.println("Factorial of " + n + " is " + factorial(n));
 13    }
 14  
 15    /** Return the factorial for a specified number */
 16    public static long factorial(int n) {
 17      if (n == 0) // Base case
 18        return 1;
 19      else
 20        return n * factorial(n - 1); // Recursive call
 21    }
 22  }