Factorial using loop in python

Here you will get python program to find factorial of number using for and while loop.

Factorial of a number is calculated by multiplying it with all the numbers below it starting from 1.

For example factorial of 4 is 24 [1 x 2 x 3 x 4].

Below program takes a number from user as an input and find its factorial.

Using For Loop

num=int[input["enter a number: "]]

fac=1

foriinrange[1,num+ 1]:

fac=fac*i

print["factorial of ",num," is ",fac]

Output

enter a number: 5
factorial of 5 is 120

Using While Loop

num=int[input["enter a number: "]]

fac=1

i=1

whilei

Chủ Đề