Factorial in Python - iteratively


Factorial iteratively

n = int(input('n = '))
i = 1
s = 1
while i <= n:
    s *= i
    i += 1
print(n , '! =', s)



 In the first line of the program, using the input() function, we read an integer (int type) from the standard input (keyboard), which we immediately store in the variable n. The input() function simultaneously displays on the screen the text given as its argument. Line 1 performs the input/output operation contained in (2) of the algorithm block. The start block (1) has no equivalent in Python.
 In lines 2 and 3, we declare the variables i and s, giving them initial values (in both cases equal to 1) (3).
 The while statement on line 4 is equivalent to the selection block (4) of our algorithm. The statements, written with mandatory indentation in Python, contained in lines 5 - 6 (block 5) will be executed as long as the condition in the while statement is met, i.e. until i is less than or equal to n. statements after the while loop, starting on line 7, where the result (6) is printed, using the standard print() function.
 This is where our program ends. There is no separate statement in Python that tells you when a program ends.