Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Python program to find factorial of a number|in 5 ways

Posted on October 16, 2019October 16, 2019

Table of Contents

  • Python program to find factorial of a number|in 5 ways
    • Program to find factorial- standard method
    • Program to find factorial- for loop entered by user
    • Program to find factorial- while loop (entered by the user)
    • Program to find factorial- using function
    • Program to find factorial- using recursion
    • Related

Python program to find factorial of a number|in 5 ways

In this tutorial, we will discuss the Python program to find factorial of a number|in 6 ways

Python program to find factorial of a number
Factorial calculation

In this post, we are going to learn how to find the factorial  of the given number in Python language

What is the factorial of a number (n)?

The factorial of a number (n) is the product of all positive integers from 1 up to n (n is the given number).

It is simply denoted by n!.

Example

if you want to find the factorial for number 5, you can follow this method.
Factorial of 5 will be 5*4*3*2*1=120
So, 5!=120.

Program to find factorial- standard method

Program  1

#Python program to find factorial of a number

num=6
fact=1  #Variable declaration and initilization

if num<0:
    print("Factorial does not available to negative");
elif num==0:
    print("factorial of 0 is 1")

else:
 while(num>0):
   fact=fact*num
   num=num-1

print("Factorial of the number is: ")

print(fact)

When the above code is executed it produces the following output

Factorial of the number is: 
720

In this program

  1. Integer variable num and fact are declared and initialized
  2. The program finds the factorial using the while loop
  3. Then, the program has displayed the factorial of a number using the print() function

Program to find factorial- for loop entered by user

Program  2

num=int(input("Enter a number to find factorial"))
factorial=1
if num<0:
    print("Factorial does not available to negative");
elif num==0:
    print("factorial of 0 is 1")

else:
    for i in range(1,num+1):
      factorial=factorial*i

    print("Factorial of the",num," is: ",factorial)

When the above code is executed it produces the following output

Enter a number to find factorial5
Factorial of the 5 is:  120

The program allows the user to enter a value and it finds and displays factorial of the given number using for loop in  Python language

Program to find factorial- while loop (entered by the user)

Program  3

num=int(input("Enter a number to find factorial: "))
fact=1

if num<0:
    print("Factorial does not available to negative");
elif num==0:
    print("factorial of 0 is 1")

else:
 while(num>0):
   fact=fact*num
   num=num-1

print("Factorial of the number is: ",fact)


When the above code is executed it produces the following output

Enter a number to find factorial: 4
Factorial of the number is: 24

The program allows the user to enter a value and it finds and displays factorial of the given number using while loop in Python language

Program to find factorial- using function

Program  4

#Python program to find factorial of a number
def factorial(num): #Function definition
   fact=1# variable initialization
   if num<0:
    print("Factorial does not available to negative");
   elif num==0:
    print("factorial of 0 is 1")
   else:
    for i in range(1,num+1):
       fact=fact*i
    return fact
num=int(input("Enter a number to find factorial: "))
result = factorial(num)
#function call and assign the value to result

print("Factorial of the %d = %d "%(num,result))

When the above code is executed it produces the following output

Enter a number to find factorial: 5
Factorial of the 5 = 120

The program allows the user to enter a value and it finds and displays factorial of the given number using the function in Python language

Program to find factorial- using recursion

Program  5

#Python program to find factorial of a number using recursion
def recur_Findfact(n):#function definition
    if n==1:
        return n
    else:
        return n*recur_Findfact(n-1)

num=int(input("Enter a number to find factorial"))

if num<0:
        print("Factorial does exist for negative number ")
elif num==0:
        print("The factorial of 0 is 1 ")
else:
        print("The factorial of",num," is ",recur_Findfact(num))

When the above code is executed it produces the following output

Enter a number to find factorial6
The factorial of' 6 is: 720

The program allows the user to enter a value and it finds and displays factorial of the given number using the recursive function in Python

Suggested post

For loop in Python

while loop in Python

Function in Python

 

Similar post

C program to find factorial of a number

Java program to find factorial of a number

Python program to find factorial of a number

C++ program to find factorial of a number

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes