• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
EDUCBA

EDUCBA

MENUMENU
  • Resources
        • Java Tutorials

          • Cheat Sheet Java
          • Cheat Sheet Python
          • C# vs Js
        • Java Tutorials
        • Python Tutorials

          • Angular 5 vs Angular 4
          • Careers in Python
          • Kali Linux vs Ubuntu
        • Python Tutorials
        • Top Differences

          • Cheat Sheet JavaScript
          • Python Interview Questions
          • Cloud Computing or Virtualization
        • Top Differences
        • Others

          • Resources (A-Z)
          • Top Interview Question
          • Programming Languages
          • Web Development Tools
          • HTML CSS Tutorial
          • Technology Basics
          • Technology Careers
          • View All
  • Free Courses
  • All Courses
        • Certification Courses

          Software Development Course 2
        • All in One Bundle

          All-in-One-Software-Development-Bundle
        • Become a Python Developer

          Python-Certification-Training
        • Others

          • Java Course
          • Become a Selenium Automation Tester
          • Become an IoT Developer
          • Ruby on Rails Course
          • Angular JS Certification Training
          • View All
  • 600+ Courses All in One Bundle
  • Login

Factorial in Python

Home » Software Development » Blog » Python Tutorials » Factorial in Python

Factorial in Python

Introduction to Factorial in Python

For a positive integer, Python in factorial means a product of all the integers which are listed less than and equal to the specified integer. The factorial value of a explicit number is typically represented as n!. the formula behind the product of these integers can be represented using the below formula,

n! = n * (n-1) * (n-2) * (n-3) * (n-4) * (n-5) * (n-6) * (n-7) * . . . . . . .* 1

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Ex: 20! = 20 * 19 * 18 * 17 * 16 * 15 * 14 * 13 * 12 * 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 =  2432902008176640000

n n!
0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5 040
8 40 320
9 362 880
10 3 628 800
11 39 916 800
12 479 001 600
13 6 227 020 800
14 87 178 291 200
15 1.30767E+12
16 2.09228E+13
17 3.55687E+14
18 6.40237E+15
19 1.21645E+17
20 2.4329E+18

Techniques of Factorial in Python

Technique #1 – Factorial Program

Code:

# Python program to determine the value of factorial for a given number
# modifying the value keyed in will produce a different result
Number = int(input(" Enter the number for which factorial value to be determined : "))
factorial = 1
# to verify that the given number is greater than zero incase it is less than zero then the
# message stated below will be printed
if Number < 0:
print(" ! ! ! ! ! Factorial value cannot be intended for negative integers ! ! ! ! ! ")
# The default factorial value for zero is one and this is printed here
elif Number == 0:
print(" ! ! ! ! 1 is the factorial value 0 ! ! ! ! ")
else:
# For loop to handle the factorial calculation
for i in range(1,Number + 1):
factorial = factorial*i
print("The factorial value for the " , Number , "is" , factorial)

Output:

Factorial in Python

Explanation: The program calculates the factorial of a number using looping technique,  here the specific integer value for which the factorial value has to be calculated is keyed in into the ‘Number’ variable. Alongside the variable ‘Factorial’ is initialized with value 1. The first check carried out is to settle on whether the key value is a positive integer. this is because the factorial value for a negative integer cannot be calculated. so the check is implied such that the keyed in value is greater than zero. also if the value keyed is equal to zero then the factorial value for zero which is one is printed. In the next instance, the factorial for a given value is determined by the below formula being executed in a loop with the iterator value getting incremented by one.

factorial = factorial*i

The range of this loop is maintained between 1 and one value greater than the number being the keyed in.  At the end of the last execution, the value of the factorial is printed.

Popular Course in this category
Cyber Week Sale
Python Certification Training (36 Courses, 12+ Projects) 36 Online Courses | 12 Hands-on Projects | 187+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.8 (3,488 ratings)
Course Price

View Course

Related Courses
Programming Languages Training (41 Courses, 13+ Projects)Angular JS Certification Training (9 Courses, 5+ Projects)

Technique #2 – Factorial Program

Code:

# Python program to determine the value of factorial for a given Number
# modifying the value keyed in will produce a different result
# Function through which factorial is achieved
def factorial(Number):
"""Factorial of a number is calculated through the below mentioned recursive function"""
if Number == 1:
return Number
else:
return Number * factorial(Number - 1)
# Number for which the factorial has to be determined
Number = int(input(" Enter the Number for which factorial value to be determined : "))
# to verify that the given Number is greater than zero in case it is less than zero then the
# message stated below will be printed
# An error message will be returned if the keyed in input is negative.
# elif an error message will be returned if the keyed in input is zero.
# else user defined function is used for calculating the factorial
if Number < 0:
print( " ! ! ! ! ! Factorial value cannot be intended for negative integers ! ! ! ! !" )
elif Number == 0:
print( " ! ! ! ! 1 is the factorial value 0 ! ! ! ! " )
else:
print("Factorial value for the ", Number , " is: " , factorial(Number))

Output:

Factorial in Python 2.png
Explanation: The program calculates the factorial of a number using a recursive function calling technique,  here the value for which the factorial needs to be determined is keyed into the ‘Number’ variable. Value 1 is initialized to the factorial variable. The first check carried out is to settle on whether the keyed value is a positive integer. this is because the factorial value for a negative integer cannot be calculated. so the check is implied such that the keyed in value is greater than zero. also if the value keyed is equal to zero then the factorial value for zero which is one is printed. In the next instance, the factorial for a given value is determined by the below formula being recursively executed,

Number * factorial(Number - 1)

recursive execution by process means a technique through which looping of a given coding instance is achieved manually. this technique involves calling a given function within the same function and this call is encapsulated inside a given if condition. So this encapsulation allows the function to be called until the given condition being satisfied.

Conclusion

These programs are implied to check whether the given palindrome of a given integer value.  Using the above programs any given numeric value can be successfully evaluated for its factorial value. the programs are implied using two widely difference techniques such as recursive function call and usual looping process. from a standard perspective, both these techniques don’t largely differ and they are very much accepted methods of programming.

 Recommended Article

This has been a guide to Factorial in Python. Here we discuss Introduction to Factorial in Python and Different Techniques of the factorial program with Example. You can also go through our other suggested articles to learn more –

  1. Patterns in Python
  2. Advantages of Python
  3. Python Frameworks
  4. Python Compilers
  5. Guide to Factorial in PHP
  6. Factorial in Java (With Methods)

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Reader Interactions
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar
Technology Blog Tutorials
  • Python Tutorials
    • Inheritance in Python
    • String Formatting in Python
    • Sorting in Python
    • Python List Comprehension
    • Arrays in Python
    • Socket Programming in Python
    • List Comprehensions Python
    • Python Regex
    • Matplotlib In Python
    • If Statement in Python
    • Dictionary in Python
    • Object in Python
    • Python Overloading
    • Reverse Number in Python
    • Fibonacci Series in Python
    • Python Keywords
    • Python Sets
    • Bubble Sort in Python
    • Heap Sort in Python
    • Python Data Types
    • Recursive Function in Python
    • Do While Loop in Python
    • Multidimensional Array in Python
    • Python Variable Types
    • Comments in Python
    • Python Features
    • Python Variables
    • Python Database Connection
    • While Loop in Python
    • Destructor in Python
    • Best Compiler for Python
    • Python IDE for Windows
    • Pandas.Dropna()
    • Math Functions in Python
    • Python Infinite Loop
    • Abstract Class in Python
    • String Array in Python
    • Python Editors
    • List Operations in Python
    • Python Nested Loops
    • Loops in Python
    • Swapping in Python
    • Python Bitwise Operator
    • Palindrome in Python
    • For Loop in Python
    • Factorial in Python
    • Encapsulation in Python
    • Python Exception Handling
    • Python File Operations
    • Random Number Generator in Python
    • Star Patterns in Python
    • Python Libraries For Data Science
    • If Else Statement in Python
    • Boolean Operators in Python
    • Constructor in Python
    • Python Comparison Operators
    • 2D Arrays In Python
    • Patterns in Python
    • Pointers in Python
    • 3d Arrays in Python
    • Python Collections
    • Advantages of Python
    • Is Python Object Oriented
    • How To Install Python
    • What Is Python
    • Is Python Open Source
    • Python Operators
    • Limitations of Using Python
    • Python Socket Programming
    • Violent Python Book Review
    • Python Programming
    • New Future of Python
    • Python Programming for Non Engineering
    • Python Programming
    • Gray Hat Python: Security
    • Python Fast And python psyco
    • Python Squeezes the Web
    • Python Programming
    • Python and Django for Web Development
    • Bash Scripting Programming and Python
    • Careers in Python
    • Uses of Python
    • Cheat Sheet Python
    • Uses Of Django
    • Sequences in Python
    • Python Programming for Absolute
    • Is Python a scripting language
    • Introduction To Python
    • Python Alternatives
  • Database Management (71+)
  • Ethical Hacking Tutorial (33+)
  • HTML CSS Tutorial (47+)
  • Installation of Software (54+)
  • Top Interview question (188+)
  • Java Tutorials (196+)
  • JavaScript (71+)
  • Linux tutorial (32+)
  • Network Security (85+)
  • Programming Languages (232+)
  • Software Development Basics (321+)
  • Software Development Careers (38+)
  • SQL Tutorial (33+)
  • String Functions (12+)
  • Technology Commands (38+)
  • Top Differences (368+)
  • Web Development Tools (33+)
  • Mobile App (60+)
Technology Blog Courses
  • Python Certification Course
  • Programming Languages Courses
  • Angular JS Certification Training
Footer
About Us
  • Who is EDUCBA?
  • Sign Up
  •  
Free Courses
  • Free Course Programming
  • Free course Python
  • Free Course Java
  • Free Course Javascript
  • Free Course on SQL
  • Free Course on Web Design
  • Free HTML Course
  • Free Android App Development Course
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • Ruby on Rails Course
  • ASP.NET Course
  • VB.NET Course
  • Bootstrap Training Course
  • Become a Linux System Administrator
  • PHP Course
  • Joomla Training
  • HTML Course
Resources
  • Resources (A To Z)
  • Java Tutorials
  • Python Tutorials
  • Top Differences
  • Top Interview Question
  • Programming Languages
  • Web Development Tools
  • HTML CSS Tutorial
  • Technology Basics
  • Technology Careers
  • Ethical Hacking Tutorial
  • SQL Tutorials
  • Digital Marketing
Apps
  • iPhone & iPad
  • Android
Support
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions

© 2019 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Let’s Get Started
Please provide your Email ID
Email ID is incorrect

Cyber Week Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) View More

Cyber Week Offer - Cyber Week Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) View More