In this post you will see that how to make a program in Python which calculates factorial of a natural number, and how it works.
You will see that how program is made line by line.
First you will define a variable fact with value 1. That will refer value of factorial throughout the program.
fact=1
Second variable you will define a variable i with value 1. That will refer value of counter for while loop.
i=1
Now, third line while i<=4: is not a simple statement, however, it is a compound statement and contains two more statements fact=fact*i and i=i+1. Statements inside while loop run 4 times. Further, while loop does the main task and calculates factorial.
while i<=4:
fact=fact*i
i=i+1
The last statement is print(fact) which will print the value of factorial which is 24.
Now if you see the final code that would be.
fact=1
i=1
while i<=4:
fact=fact*i
i=i+1
print(fact)
To understand it very clearly, see the below video in which working of program explained line by line using animation.