Python Program to find the Factorial of a number

python program to find FACTORIAL OF NUMBER

Find the Factorial of the Number in Python

Given an integer input the objective is to Find the Factorial of the Given Number using loops and recursion. Therefore, we write a code to Find the Factorial of a Number in Python Language.

Example
Input : 5
Output : 120

Find the factorial of the Number in Python Language

Given an integer input, the objective is to find the factorial of the given integer input using loops and recursion. We keep on multiplying the number with it’s previous number until the previous number is 0. Here are some of the methods to Find the factorial of the Number in Python Language,

  • Method 1: Using Iteration 
  • Method 2: Using Recursion

We’ll discuss the above mentioned methods in detail in the upcoming sections.

Method 1: Using Iteration 

Working

In this method we’ll use for loop to iterate through the numbers until the number whose factorial we’re trying to find, meanwhile multiplying the number to the output variable.

For an integer variable as an input, we preform the following operations,

  • Run a for loop from 1 until the number+1.
  • Keep multiplying the numbers to the output variable.
  • Print the output variable.

Let’s implement the above mentioned logic in Python Language.

Python Code

num = 5
output = 1
for i in range(2,num+1):
  output*=i
print(output)

Output

120

Method 2: Using Recursion

Working

In this method we’ll use recursion to calculate the factorial of the number input by multiplying all the number until the number is 1. To know more about recursion check out our page, Recursion in Python .

For a Given integer input as num, we perform the following operations,

  • Define a recusive function recurfact() that takes the number as an argument.
  • Set the base case as num==1.
  • Set the step recursive call as num * recurfact(num-1).
  • Print the returned value as the output.

Let’s implement the above logic in python language.

Python Code

num = 5
def recurfact(num):
  if num==1:
    return 1
  return num* recurfact(num-1)
print(recurfact(num))

Output

120

10 comments on “Python Program to find the Factorial of a number”


  • Devil

    import math
    a = int(input (“enter the number :”))
    b = math.factorial(a)
    print(b)
    Output:
    enter the number : 4
    24


  • VICKY

    import math
    def fact(n):
    a = math.factorial(n)
    print(a)

    a = int(input(“enter a number”))
    fact(a)


  • asatiayush0

    def factorial(n):
    return 1 if n == 1 else n * factorial(n-1)
    print(factorial(int(input(“Enter number: “))))


  • sandesh

    def fac(i):
    if i==0:
    return 1
    if i>0:
    return i*fac(i-1)
    n=int(input(‘enter which no fact you want ‘))

    f=fac(n)
    print(f)


  • Afaque

    x=int(input(‘Enter the number : ‘))
    fac=lambda x: 1 if x<1 else x* fac(x-1)
    print(fac(x))


  • Rahul

    n=int(input(“enter your number:”))
    count=1
    for i in range (1,n+1):
    count=count*i
    print(f”The factorial of number is: {count}”)


  • Slayer166

    def fact(n):
    if(n==0):
    return 0
    if(n==1):
    return 1
    return n*fact(n-1)
    n=int(input())
    print(fact(n))