use a for loop, and have an answer variable that is set to 1. then decrement the for loop, and then multiply the number in the for loop to the number in the answer, and once you are done return the answer.
no. instead of adding you want to subtract. and the n is the factorial (in your case 7, so 7 x 6 x 5 x 4 x 3 x 2 x 1). so you want to use n in your for loop. and you want to decrement to follow the pattern. then your fact value if your answer, so the operation would look like this
no. instead of adding you want to subtract. and the n is the factorial (in your case 7, so 7 x 6 x 5 x 4 x 3 x 2 x 1).
I don't understand why you have rejected the OP's solution just because it computes the answer in the order 1 x 2 x 3 x 4 x 5 x 6 x 7. The end result is that same.
@xiangzhuang: "I just did this but can you guys please explain why its this?"
That's a very strange question. If you just did that you must know why you did it that way. Is it possible that you copied that code form someone/somewhere else?
I'm sorry I didn't notice that.. It doesn't make a difference because when you multiply the order doesn't matter. it is only when you are adding and multiplying in the same command. for example if you multiply 5 x 4 x 3 you get 60, and if you do (5 x 5) + 5 which gives an answer of 30, or if you do 5 * (5 + 5) which gives you an answer of 50. This is the reason BEDMAS was created.
Also the reason I rejected it was because factorials go from the highest number down. So I wanted to keep the factorial method the same as what a factorial is, but it doesn't actually matter.
@carlosreg: That's just a scruffy version of the code that xiangzhuang posted earlier in this thread, except that his version works and yours does not.
You can avoid much embarrassment by (a) reading the whole thread and (b) testing your code for elementary errors before posting.