# How to factorial each number inside an array

I'm trying to factorial each number inside an array. can someone point me in the right direction on how to do this

``````    for(int i=2; i < limit; i++){
series[i] = series[i-1] + series[i-2]
series[i]= series[i] *i;
}
``````

If I understood you correctly you are looking for the following list :

`[1!, 2!, 3!, 4!, ...]`

So basically : `a(n) = a(n-1) * (n+1)`, starting from `n = 1`.

Make a method to calculate the factorial of each number in your series.

``````public static void main(String... args) {
int[] series = {3, 5, 7, 9};
for (int i = 0; i < series.length; i++) {
series[i] = factorial(series[i]);
System.out.println(series[i]);
}
}

private static int factorial(int n) {
int result;
if (n == 0) {
result = 1;
} else if (n < 0) {
throw new IllegalArgumentException("Unable to calculate n! for n = " + n);
} else {
result = n;

for (int i = n - 1; i > 1; i--) {
result *= i;
}
}
return result;
}
``````

You can try recursion to shorten the code it takes to calculate one number's factorial.

``````public static long factorial(int N)
{
if(N == 1) return 1;
return N * factorial(N-1);
}
``````

That will give you the factorial for any number N. The next step is to call this function for each number in the array. (Not sure how to do that yet - but I hope the recursion function can help you at least to calculate one number's factorial)

Using `IntStream` makes mapping `int` arrays a bit simpler:

``````series = Arrays.stream(series).map(this::factorial).toArray();
``````

This assumes a method such as:

``````private int factorial(int n) {
return IntStream.rangeClosed(1, n).reduce(1, (n1, n2) -> n1 * n2);
}
``````

This may seem complicated but if you take a look at the `IntStream` documentation.