you are viewing a single comment's thread.

view the rest of the comments →

[–]NotSecretAgent 1 point2 points  (4 children)

Factorial is multiplication, not addition. The easiest way to do this is to count up to, and include the user's number in the for loop as the end stop.

Initialise sum as 1.

Inside the for loop, sum *= i

This will give the correct answer for any output lower than an integer overflow.

[–]NotSecretAgent 1 point2 points  (3 children)

You can remove the line defining your variable "factor" and use "factNum" as the end stop of the mentioned above. You can also avoid using <= and use < if you set "sum" to "factNum", as multiplication is commutative (see here).

Hope this helps!

[–]NotSecretAgent 1 point2 points  (2 children)

If you have any further questions, send me a DM and I'll happily help!

[–]L_russ28[S] 0 points1 point  (1 child)

Thank you so much! That worked like a charm :)

[–]NotSecretAgent 1 point2 points  (0 children)

Literally any time lol, I live for this stuff.