Write algorithm to print y=4! + 6! + 8!+ 10!

i need help with factorial .

### Best Answer:

**Hamza:** public static void main (String[] args) {

int sum = 0;

for (int n=4; n<=10; n+=2) {

sum += fact(n);

}

System.out.println("Sum of factorials is "+sum);

}

public static int fact(int n) {

int f = 1;

for (int i=n; i>1; i–) {

f *= i;

}

return f;

}

### Other answer:

**Hamza:**

In computing the value of 10! = 1*2*3*4*5*6*7*8*9*10, along the way you are computing all of the other factorials you need. Why not take advantage of that?

sum = 0

fn = 1

for n = 2 to 10 do

…. fn = fn*n — fn is n! on each iteration of the loop

…. if (n is even) and (n>=4) then

…. …. sum = sum + fn

…. end if

end for

output "the sum is: ", sum

That's pseudocode, but you should get the idea of what's going on. [edit: …and broken pseudocode in the first version…should be a bit better now; and you're supposed to be working out the details anyway, right?]

If you want to optimize that a bit more, loop in steps of 2 so n takes on values 2, 4, …, 10, and then use n! = n*(n-1)*(n-2)!.

for n = 2 to 10 step 2 do

…. fn = (n-1) * n * n

…. if n>=4 then

…. …. sum = sum + fn

…. end if

end for

**joe:**

Really, are you too lazy to do your homework?

**Abraham:**

A

**Adam:**

A

**Felicity:**

A

**reghar:**

A