Write an iterative function that returns the double factorial n!! for a natural n.
Recall that n!! = n × (n − 2) × (n − 4) × …. For instance, 9!! = 9 × 7 × 5 × 3 × 1 = 945 and 8!! = 8 × 6 × 4 × 2 = 384. By definition, 0!! = 1!! = 1.
Interface
C++,C | int double_factorial(int x); |
Java | public static int doubleFactorial(int x); |
Python | double_factorial(x) # returns int |
double_factorial(x: int) -> int |
Precondition
Assume 0≤ n ≤ 19.
Observation You only need to submit the required procedure; your main program will be ignored.
Input/Output