226k views
4 votes
For the question below, refer to the following recursive factorial method.

public int factorial(int x)
{
if (x > 1)
return x * factorial (x - 1);
else
return 1;
}

What condition defines the base case for this method?

a) (x > 1)
b) (x = = 1)
c) (x = = 0)
d) (x <= 0)
e) (x <= 1)

1 Answer

4 votes

Answer:

B) (x<=1)

Step-by-step explanation:

Though this code given will run perfectly without any requirement of base case.But still we have to include a base case I will suggest (x<=1) because the value of x is decreased by one at each recursive call so it can handle it very well and it can handle negative values of x also if user enters it by mistake since factorial is for positive numbers only.

User Lammert
by
5.4k points