No one here is interested in making fun of you.
We are all here because we had to learn this very same stuff ourselves, and remember how hard it was.
Factorial is defined as:
n! = 1 * 2 * ... * (n-1) * n
A concrete example is:
5! = 1 * 2 * 3 * 4 * 5
In order to calculate this with pen and paper, one might start combining terms:
5! = 1 * 2 * 3 * 4 * 5
↓ ↓ | | |
----- | | |
2 | | |
↓ ↓ | |
------- | |
6 | |
↓ ↓ |
-------- |
24 |
↓ ↓
---------
120
|
This is exactly what your program must do.
First, notice that the 5 (as in
5!
) never changes. It is always a five. Your loop will always go 1→5.
Next, notice that the products are distinct things too...
n is your 5. It doesn't change.
x is your 1,2,3,... It changes on every iteration of your loop.
y is your running product. First it is 1*1. Then it is 1*2. Then it is 2*3:
y=1 // (initial value)
y*=1 // y==1
y*=2 // y==2
y*=3 // y==6
y*=4 // y==24
y*=5 // y==120 |
Your final y is the final product.
Hope this helps.