WSQ09 – factorial calculator

this time the program has to calculate the factorial (n!) of a non-negative number given by the user.

first we have to understand what factorial is. it is the multiplication of all the numbers from 1 to n.

n!= 1x2x3x4x5x. . . . . xn!

now we could use the python function for factorial  math.factorial(n) but lets do math.

first ask the user for a positive integer and set the folowing variables:

n = int(input())

f = 0  – – – – this is a counter

c = 1   – – – – this will be the factorial product

now we can do this with a while or a for loop, for the while loop we could put it like this:

while f < n:

       f += 1

       c = c*f

and the for loop:

for t in n:

        f += 1

       c = c*f

CC BY 4.0 WSQ09 – factorial calculator by sergio is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.