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.
Python Program to Find Factorial of Number
Using For Loop
num = int(input("enter a number: ")) fac = 1 for i in range(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 while i <= num: fac = fac * i i = i + 1 print("factorial of ", num, " is ", fac)
Output
enter a number: 4
factorial of 4 is 24
Comment below if you have any queries related to above python factorial program.
The post Python Program to Find Factorial of Number Using Loop appeared first on The Crazy Programmer.