Answer:
public static int factorial(int n) {
if (n >= 1 && n <=12) {
if (n==1)
return 1;
else
return n * factorial(n-1);
}
else
return -1;
}
Explanation:
Create a method called factorial that takes one parameter, n
Check if n is n is between 1 and 12. If it is between 1 and 12:
Check if it is 1. If it is 1, return 1. Otherwise, return n * function itself with parameter n-1.
If n is not between 1 and 12, return -1, indicating that the number is not in the required range.
For example:
n = 3 Is n==1, NO factorial(3) = n*factorial(2)
n = 2 Is n==1, NO factorial(2) = n*factorial(1)
n = 1 Is n==1, YES factorial(1) = 1.
Then factorial(2) = 2*1 = 2, factorial(3) = 3*2 = 6