I tried the following challenge:
Given the first few factorials:
1! = 1
2! = 2 x 1 = 2
3! = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 24
What is the sum of the first 15 factorials, NOT INCLUDING 0!?
My solution in Java is the following:
public class Factorial
{
public static void main(String[] args)
{
int sum = 0;
int multi = 1;
for (int i=1;i<=15;i++)
{
multi = multi*i;
sum = multi+sum;
}
System.out.print(sum);
}
}
I verified the solutions for the first 7 factorials but will it work for the first 15?
No, its not working for 15. Use long. Also, you could move the print statement inside the loop, to check from where it starts failing. I guess in this case it's 13.