# Python Program to Find Factorial of Number Using Loop

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

Output

enter a number: 5
factorial of 5 is 120

### Using While Loop

Output

enter a number: 4
factorial of 4 is 24

### Video Tutorial

Comment below if you have any queries related to above python factorial program.

Category: Python Programming A crazy computer and programming lover. He spend most of his time in programming, blogging and helping other programming geeks.

## 17 thoughts on “Python Program to Find Factorial of Number Using Loop”

1. HIMANISH SHEKHAR DAS

why num+1 in for loop ?

1. Admin Post author

Because range(start, end) function will generate numbers from start and end-1. In above program we want to generate numbers from 1 to num, I have used num+1 that means (num+1)-1 which ultimately means num.

2. Rachit Gupta

As we know that if we write only num,it will take range (1,num-1)means if we write only num it will range from 1 to num -1. And if we range (1,num+1)it range the number also that you have entered.

**THAT’S THE SOLUTION**

3. Mukesh

Because if you give only num it will end with 1 less number
Eg.for i in range(1,5)
Print(i)
1
2
3
4
It will olny print 4 number there fore we add num+1

2. deepak

def factorial(a):
result=1
for i in range(1,a+1):
result= result*i
return result
a = input(‘ur number please ‘)
y = (factorial(a))
print(y + str (‘is ur answer’))
can any body tell me where is the problem

1. RUPESH

indentation problem
result must be under the loop.
press tab button on reach lines from result

2. Namit Vithalrao Shastri

You first define function and then you didn’t use it . So off course no output. Example :- factorial(4)

3. sven Brodowski

Hi
i’m Sven Brodowski
my english is not verry good

youre programms are super
I’m learning Python and Java

my Questions
have you got a programm in Python
where can do inverse Matrix

I live in heilbronn germanny

ok see you later

4. Paula Odell

Hello!

I have read my book from Codio and have attempted the challenge. Here is what I have coded:

# Get N from the command line
import sys
N = int(sys.argv)

N!=N*(N-1)*(N-2)*(N-3)*(N-4)*1
print (N)

It is telling me my output is 4.
I am confused, because if N is 4 based upon what the user input, why is it not completing the calculation?

Can you help?

5. libr

import math
x = math.factorial(2)
print(x)

6. Aditya

An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100.

Output
For each integer n given at input, display a line with the value of n!
Find the factorial!!

7. chkundankanth

python program on factorial using recurssion?

1. sujit

def factorial(a):
if a == 0 or a ==1:
return a
else:
return a * factorial(a-1)

a = int(input())
y = factorial(a)
print( y)

8. newtoPython

With the for loop, how would I be able to have it display the actual calculation along with the answer, for example if user enters 4, I need it to display:

4! = 1 x 2 x 3 x 4 = 24

I am able to get the code to display the answer, however not the actual equation.

9. Robert

Thank you! This was very helpful.

10. Ronak Panchal

how to find if factorial is even or odd

11. Kumar

num = int(input(“enter a number: “))

fac = 1

for i in range(1, num):
fac = fac *num
num = num-1
print(“factorial of “, num, ” is “, fac)