Factorial. In mathematics, the factorial of an integer n, denoted by n!, is the product of all positive integers from 1 to n:
|
n = int(input()) factorial = 1 for i in range(1, n + 1): factorial *= i print(factorial) |
- Reading Input:
- The
input()
function reads a line of text from the user.
- The
int()
function converts this input to an integer and stores it in the variable n
.
- Initialize the Factorial:
- A variable
factorial
is initialized to 1. This will be used to store the product of all positive integers up to nnn.
- Calculate Factorial:
for i in range(1, n + 1):
factorial *= i
- A
for
loop iterates over the range from 1 to nnn (inclusive).
- In each iteration, the current number iii is multiplied with
factorial
, updating its value.
- Print the Factorial:
- Finally, the
print()
function outputs the value of factorial
.