How to print factorial of a number in Python

In this python tutorial, you will learn how to print factorial of a number in Python and the Python program to print factorial of a number, also we will check:

  • Python program to print factorial of a number
  • Python program to print factorial of a number using function
  • Python program to print factorial of a number using for loop
  • Python program to find factorial of a number using recursion
  • Python program to print factorial of a number by getting input from a user
  • Python program to find factorial of a number using while loop
  • Python program to print factorial of a number without recursion
  • Factorial program in python using if-else
  • Python program to print factorial of number 4
  • Python program to print factorial of number 10

Print factorial of a number in Python

Let’s see python program to print factorial of a number.

  • Firstly, the number whose factorial is to be found is stored in Num.
  • Declare and initialize the factorial variable to 1.
  • We will use an if-else statement to check whether the number is negative, zero, or positive.
  • The for loop and range() function is used if the number is positive to calculate the factorial of a number.
  • At last, print is used to get the factorial of a number.

Example:

Num = 5
Factorial = 1
if Num < 0:
    print("Factorial does not exist for negative numbers")
elif Num == 0:
    print("The factorial of 0 is 1")
else:
    for i in range(1, Num + 1):
        Factorial = Factorial * i
    print("The factorial of",Num,"is",Factorial)

We can see that the factorial of a number is 120 in the output. You can refer to the below screenshot for the output.

Python program to print factorial of a number
Python program to print factorial of a number

The above code, we can use to print factorial of a number in Python.

Python program to print factorial of a number using function

Now, we will see python program to print factorial of a number using function.

  • In this example, we have defined the function as factorial(num).
  • To find the factorial we have used a single line if-else statement and also we have assigned the number as 6 for whose factorial is to be found.
  • At last, print is used to get the factorial of a number.

Example:

def factorial(num):
    return 1 if (num==1 or num==0) else num * factorial(num-1);
number = 6
print("The factorial of",number,"is",factorial(number))

We can see that the factorial of a number is 720 in the output. You can refer to the below screenshot for the output.

Python program to print factorial of a number using function
Python program to print factorial of a number using function

This is how to print factorial of a number using function in Python.

Python program to print factorial of a number using for loop

Here, we will see python program to print factorial of a number using for loop

  • Firstly, we will take the input from the user whose factorial is to be found.
  • Declare and initialize the Fact variable to 1.
  • The for loop and range() function is used to calculate the factorial of a number.
  • At last, print is used to get the factorial of a number.

Example:

num = int(input("Enter a number:"))
Fact = 1
for i in range(1, num + 1):
    Fact = Fact * i
print("The Factorial of",num,"is",Fact)

We can see that the factorial of a number is 24 in the output. You can refer to the below screenshot for the output.

Python program to print factorial of a number using for loop
Python program to print factorial of a number using for loop

The above code, we can use to print factorial of a number using for loop in Python.

Python program to find factorial of a number using recursion

Now, we will see python program to find factorial of a number using recursion.

  • Here, we will use recursion to find the factorial of a number.
  • In this example, we have defined the function as recursion_factorial(n)
  • We will store the value in number. We will use an if-else statement to check whether the number is negative, zero, or positive.
  • The number is passed to the recursion_factorial() function to compute the factorial of the number.

Example:

def recursion_factorial(n):
   if n == 1:
       return n
   else:
       return n*recursion_factorial(n-1)
number = 8
if number < 0:
   print("Factorial does not exist for negative numbers")
elif number == 0:
   print("The factorial of 0 is 1")
else:
   print("The factorial of", number, "is", recursion_factorial(number))

We can see that the factorial of a number is 40320 in the output. You can refer to the below screenshot for the output.

Python program to find factorial of a number using recursion
Python program to find factorial of a number using recursion

The above code is to find factorial of a number using recursion in Python.

Python program to print factorial of a number by getting input from a user

Let see python program to print factorial of a number by getting input from a user

  • Firstly, we will take the input from the user whose factorial is to be found.
  • Declare and initialize the fact variable to 1.
  • We will use an if-else statement to check whether the number is negative, zero, or positive.
  • The for loop and range() function is used if the number is positive to calculate the factorial of a number.
  • At last, print is used to get the factorial of a number.

Example:

num = int(input("Enter a number:"))
fact = 1
if num < 0:
    print("Factorial does not exist for negative numbers")
elif num == 0:
    print("The factorial of 0 is 1")
else:
    for i in range(1,num+1):
        fact = fact * i
print("The Factorial of",num,"is",fact)

We can see that the factorial of a number is 5040 in the output. You can refer to the below screenshot for the output.

Python program to print factorial of a number by getting input from a user
Python program to print factorial of a number by getting input from a user

This is the Python program to print factorial of a number by getting input from a user.

Python program to find factorial of a number using while loop

Here, we will see python program to find factorial of a number using while loop.

  • Firstly, we will take the input from the user whose factorial is to be found.
  • Declare and initialize the fact variable to 1.
  • We will use an if-else statement to check whether the number is negative, zero, or positive.
  • A while loop is used to multiply the number to find the factorial in a continuing process.
  • The process continues until the value of the number is greater than zero.
  • At last, print is used to get the factorial of a number.

Example:

num=int(input("Enter a number to find factorial: "))
fact=1;
if num<0:
   print("Factorial does not defined for negative integer");
elif(num==0):
    print("The factorial of 0 is 1");
else:
  while(num>0):
        fact = fact*num
        num=num-1
print("Factorial of the given number is: ",fact)

We can see that the factorial of a number is 120 in the output. You can refer to the below screenshot for the output.

Python program to find factorial of a number using while loop
Python program to find factorial of a number using while loop

The above code, we can use to find factorial of a number using while loop in Python.

Python program to print factorial of a number without recursion

Here, we will see python program to print factorial of a number without recursion.

  • Firstly, we will take the input from the user whose factorial is to be found.
  • Declare and initialize the fact variable to 1.
  • A while loop is used to multiply the number to find the factorial in a continuing process.
  • The process continues until the value of the number is greater than zero.
  • At last, print is used to get the factorial of a number.

Example:

num = int(input("Enter number:"))
Factorial = 1
while(num>0):
    Factorial=Factorial*num
    num=num-1
print("The factorial of entered number is:",Factorial)

We can see that the factorial of a number is 3628800 in the output. You can refer to the below screenshot for the output.

Python program to print factorial of a number without recursion
Python program to print factorial of a number without recursion

The above code is to print factorial of a number without recursion in Python.

Factorial program in python using if-else

Now, we will see factorial program in python using if-else

  • Firstly, we will take the input from the user whose factorial is to be found.
  • Declare and initialize the fact variable to 1.
  • We will use an if-else statement to check whether the number is negative, zero, or positive.
  • The for loop and range() function is used if the number is positive to calculate the factorial of a number.
  • At last, print is used to get the factorial of a number.

Example:

number = int(input("Enter a number: "))  
fact = 1  
if number < 0:  
   print("Factorial does not exist for negative numbers")  
elif number == 0:  
   print("The factorial of 0 is 1")  
else:  
   for i in range(1,number + 1):  
       fact = fact*i  
   print("The factorial of",number,"is",fact)

We can see that the factorial of a number is 40320 in the output. You can refer to the below screenshot for the output.

Factorial program in python using if-else
Factorial program in python using if-else

This is the Factorial program in python using if-else.

Python program to print factorial of number 4

Let see python program to print factorial of number 4.

  • Firstly, we will take the input from the user whose factorial is to be found.
  • Declare and initialize the Fact variable to 1.
  • The for loop and range() function is used to calculate the factorial of a number.
  • At last, print is used to get the factorial of a number.

Example:

number = int(input(" Please enter any Number to find factorial : "))
fact = 1
for i in range(1, number + 1):
    fact = fact * i
print("The factorial of",number,"is",fact)

We can see that the factorial of a number is 24 in the output. You can refer to the below screenshot for the output

Python program to print factorial of number 4
Python program to print factorial of number 4

This is code to print factorial of number 4 in Python.

Python program to print factorial of number 10

Now, we will see python program to print factorial of number 10.

  • Firstly, we will take the input from the user whose factorial is to be found.
  • Declare and initialize the Fact variable to 1.
  • The for loop and range() function is used to calculate the factorial of a number.
  • At last, print is used to get the factorial of a number.

Example:

number = 10
fact = 1
for i in range(1, number + 1):
    fact = fact * i
print("The factorial of",number,"is",fact)

We can see that the factorial of a number is 3628800 in the output. You can refer to the below screenshot for the output

Python program to print factorial of number 10
Python program to print factorial of number 10

This is the Python code to print factorial of number 10.

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Python programs to print factorial of a number. Also, we covered these below topics:

  • Python program to print factorial of a number
  • Python program to print factorial of a number using function
  • Python program to print factorial of a number using for loop
  • Python program to find factorial of a number using recursion
  • Python program to print factorial of a number by getting input from a user
  • Python program to find factorial of a number using while loop
  • Python program to print factorial of a number without recursion
  • Factorial program in python using if-else
  • Python program to print factorial of number 4
  • Python program to print factorial of number 10