How many times is the factorial method in Listing 1 invoked for factorial(6)?
LISTING 1 ComputeFactorial.java
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 nonnegative 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 the 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 }
![]()
![]()
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.