DEV Community

Cover image for Python program to get factorial of n number
Kuldeep Singh
Kuldeep Singh

Posted on • Originally published at programmingeeksclub.com

Python program to get factorial of n number

In this article of Factorial Program in Python we are going to write a python program to find the factorial of N number.

In programming there is lots of way to doing a simple or hard tasks or solving a problem, ways or you can say approaches aren’t limited in programming, but in this article the approaches we are going to use are mentioned below:

  • Factorial Program in Python using loop
  • Factorial Program in Python using recursive approach

Factorial Program in Python using Loop

def factorial(n):
  print("user input is: {}\n".format(n))

  fac = 1

  # check if the number is negative, positive or zero
  if n < 0:
    print("can't factorial of negative numbers")
    return 0
  elif n == 0:
    print("The factorial of 0 is 1")
    return 1
  else:
    for i in range(1, n + 1):
      fac = fac * i
      print("i = {}, {} * {} = {}".format(i, i, fac, fac))

  return fac


print("Factorial Program in Python")
# user input which will be passed to
# factorial function to get factorial
userInput = 7
fac = factorial(userInput)

print("\nFactorial of {} is {}".format(userInput, fac))
Enter fullscreen mode Exit fullscreen mode

As you can see in this example we’re using for range loop and we’re starting from loop from 1 to n+1 so it won’t give us any wrong value, you can even run this to get factorial of 1 and two as well and it’ll work just fine.

If you run this code, you’ll get following output:

$ python main.py
Factorial Program in Python
user input is: 7

i = 1, 1 * 1 = 1
i = 2, 2 * 2 = 2
i = 3, 3 * 6 = 6
i = 4, 4 * 24 = 24
i = 5, 5 * 120 = 120
i = 6, 6 * 720 = 720
i = 7, 7 * 5040 = 5040

Factorial of 7 is 5040
Enter fullscreen mode Exit fullscreen mode

I leave testing for negative and zero conditions on you try to run the code and comment your output in comment box.

Factorial Program in Python Using recursive function

Below is the code for how to recursively get factorial of n number in python:

# Factorial Program in Python


def factorial(n):
  # check if the number is negative, positive or zero
  if n < 0:
    print("can't factorial of negative numbers")
    return 0
  elif n == 0 or n == 1 :
    return 1
  else:
    # calling factorial function
    # so it can work recursivly
    return (n * factorial(n-1))



# user input which will be passed to
# factorial function to get factorial
userInput = 5
fac = factorial(userInput)

print("\nFactorial of {} is {}".format(userInput, fac))
Enter fullscreen mode Exit fullscreen mode

In this code if look at we’ve added both condition we used in loop and also added on more within n==0, First we are checking if given value is negative or not if negative then return back and warn user about input, in next elif we’ve added condition so whenever n becomes 1 or 0 then return from that sequence and return the 1 as factorial of it, and in last we are calling our factorial function so it can process input until n become 1 or 0, 1 is going to come first in reverse sequence as we are using minus operator to subtract 1 each time from n so in end when n becomes 1 it’ll immediately return 1 and will start multiplying each value sequentially for final return value or answer.

To understand recursive approach you need to have good understanding of how function works recursively.

You’ll see the following output whenever you run the above code:

$ python main.py
Factorial of 5 is 120
Enter fullscreen mode Exit fullscreen mode

This article is originally posted on programmingeeksclub.com

My Personal Blogging Website : Programming Geeks Club
My Facebook Page : Programming Geeks Club
My Telegram Channel : Programming Geeks Club
My Twitter Account : Kuldeep Singh

Top comments (1)

Collapse
 
5af1 profile image
Safi

Great blog. But,,,,,,,,,,,,,
PLease please please use f-strings. please.