# factorial program
n = int(input())
fact = 1
for i in range(n,1,-1): #line4 # this line print the range in reverce
fact = fact*i
print(fact)
some explain the line4. how is the for loop printing the n in reverse?
CodePudding user response:
The 3 main parameters of the range()
function are start, stop and step. If you start at n
to stop on 1 and decrease the index variable by 1 unit every time the loop executes, it will loop the values in reverse order.
For example:
n=10
for i in range(n,1,-1):
print(i)
This loop will start at i=10
, as you can see from the step value, it will decrease its value by 1 until reach the stop value (exclusive), in this case, 1 too. So the output will be:
10
9
8
7
6
5
4
3
2