Sum of Factorials

• February 10th, 2014, 10:51 AM
tmt
Sum of Factorials
Hi, I'm working on this code right now. The first method returns the factorial of an integer (factorial of 3 = 1x2x3). And the second returns the sum of the factorials between 2 integers. This is correct according to my professor. I'm just confused as to why in the 2nd methods it isn't "total = total + Factorial(i);" instead of what I have.

thanks for the help.

Code :

```public class LoopProblems { public int Factorial (int n){   int total = 1;   for (int i = 1; i <= n ; i++ ) {   total = total * i; } return total;   }   public int SumOfFactorials (int start, int end) {   int total = 1;   for (int i = start; i <= end ; i++ ) {   total = total * Factorial(i); } return total;   } }```
• February 10th, 2014, 11:29 AM
Norm
Re: Sum of Factorials
What is the output from when the posted code is executed?
x * 0 = 0
• February 10th, 2014, 11:33 AM
tmt
Re: Sum of Factorials
So I changed it to 1. Is the code correct now?

Thanks!
• February 10th, 2014, 11:45 AM
Norm
Re: Sum of Factorials
Quote:

Is the code correct now?
Hard to tell without testing it.
What happens when you compile and execute it?
• February 10th, 2014, 11:51 AM
tmt
Re: Sum of Factorials
I'm not quite sure how to do that yet. We're not quite there yet in class. I'm going to teach myself how to do that soon. We just kind of work it out logically. To me , it should be
Code :

`total = total + Factorial(i);`
Thanks!
• February 10th, 2014, 12:39 PM
GregBrannon
Re: Sum of Factorials