• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » Factorial Program in Python
Next →
← Prev

Factorial Program in Python

By Dinesh Thakur

The factorial of a non-negative integer n, denoted by n! is the product of all the integers less than or equal to that number.

factorial program in pythonSyntax for factorial number is:

n! = n.(n - 1).(n - 2).(n - 3)....3.2.1.

For example, the factorial of 5 (denoted as 5!) is

5! = 5.4.3.2.1 = 120 

The factorial of 0! is 1, according to the convention for an empty product.

The Factorial operation in many mathematical areas is used in permutations and combinations, algebra and mathematical analysis.

Factorial Program in Python

We are using three ways to calculate factorial number:

• Using a function from the math module
• Iterative approach
• Recursive approach

Factorial program in python using the function

We may import a python module known as math that contains virtually any math function. To use a function to calculate the factorial, here is the code:

# Python program to find the factorial of a given number.
# change the value for a different result
import math
# Enter the value from the user
num = int(input("Enter the number: "))
print("factorial of ",num,": ",end="")
print(math.factorial(num))

Output :

Factorial program in python using the functionFactorial program in python using for loop (Iterative approach)

def factorial(n): //function definition
fact=1
if int(n) >= 1:
   for i in range (1,int(n)+1):
     fact = fact * i
   return fact
num = int(input("Enter the Number: "))
print("factorial of ",num,": ",end="")
print(factorial(num))

Output :

Factorial program in python using for loopFactorial program in python using recursion

In this case, we are defining a user-defined function factorial(). This function finds the factorial of a given number by calling itself repeatedly until the base case reach.

# Python program to find the factorial of a number using recursion
def factorial(n): 
  if n == 1:
     return n
  else:
     return n*factorial(n-1)
# take input from the user
n = int(input("Enter the number : "))
print("factorial of ",n," : ",end="")
print(factorial(n))

Output :

Factorial program in python using recursion

You’ll also like:

  1. Factorial Program in C
  2. Factorial Program in Java
  3. C Program Find the Factorial of N Number
  4. C Program Calculate Factorial of a Number using Recursion
  5. Write A C++ Program To Find The Factorial Of A Number By Using The Recursion.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.