Factorial Program in Python using For and While Loop

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

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

For example, the factorial of 4 is 24 (1 x 2 x 3 x 4).

The below program takes a number from the user as input and finds 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

Video Tutorial

There are two other ways to find factorial in Python using recursion and math.factorial() function.

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

28 thoughts on “Factorial Program in Python using For and While Loop”

    1. 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.

      1. Sir kindly is mein help kr dein as soon as possible.
        Python program that evaluates the factorials of the integers from 1 to 5
        Print the results in tabular format as following.
        X Factorial of X
        1 1
        2 2
        3 6
        4 24
        5 120

    2. 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. 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

  1. 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. Namit Vithalrao Shastri

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

    2. This is what I used and it worked on Codio:

      t = 1
      for i in range(1, N+1):
      t = t * i
      if N – i == 0:
      print(str(t))
      else:
      continue

    3. def factorial(a):
      p=1
      for i in range(1,a+1):
      p= p*i
      return p

      a = int(input(“ur number please “))
      y = factorial(a)
      print(” Your f is”, y)

  2. 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

  3. 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[1])

    # Your code goes here
    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?

  4. 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!!

    1. 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)

  5. 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.

    Thanks for your help in advance!

    1. here the code!, might be can help you

      number = input (“give me a number =”)
      number = int (number)
      fac = 1
      i = 1
      print (f” {number}! =”, end = ” “)
      while i <= number :
      fac = fac * i
      i = i + 1
      if (i == number+1):
      print (i – 1 , end = "=")
      else :
      print (i – 1 , end = "X")

      print (f" {fac}")

  6. 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)

  7. Giovanny Guthiery Soares de Oliveira

    def fat(n):
    a = n – 1
    while a != 1:
    n *= a
    a -= 1
    print(f’O fatorial de {c} é igual a {n}’)
    c = int(input(‘Digite Um Numero: ‘))
    fat(c)

  8. While the answer is correct, its going to print several wrong answers, isn’t it better done with a conditional if to print? So this way it’s only going to print the right answer.

    like this:

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

    fac = 1

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

  9. Ask the user to enter a number between 0-20 (including 20), if the user makes an inappropriate entry, give a warning message to enter a suitable entry until the appropriate entry.
    If the number entered by the user is “odd”, calculate the factorial of the entered number and print it on the screen.
    If the number entered by the user is “even”, the sum of even numbers from 0 to the number entered by the user (including the entered number) should be written on the screen
    If the entered number is 5, the output will appear on the screen: 5! = 120
    If the entered number is 12, the output will be displayed: The sum of even numbers from 0 to 12 is 42.

Leave a Comment

Your email address will not be published. Required fields are marked *