Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python Program to find Factorial of a Number

by suresh

Write a Python program to Find Factorial of a Number using For Loop, While Loop, Functions, and Recursion. The Python Factorial denoted with the symbol (!). The Factorial of number is the product of all numbers less than or equal to that number & greater than 0.

n! = n * (n-1) * (n -2) * …….* 1

For example, Factorial of 5 represented as
5! = 5 *4 * 3 * 2 * 1 = 120

Python Program to find Factorial of a Number using Math function

In this Python factorial of a number program, we are using the built-in math function called factorial.

# Python Program to find Factorial of a Number

import math 

number = int(input(" Please enter any Number to find factorial : "))

fact = math.factorial(number)
print("The factorial of %d  = %d" %(number, fact))
Python Program to find Factorial of a Number 1

Python Program to find Factorial of a Number using For Loop

This python program for the factorial of a number allows the user to enter any integer value. Using this value, it finds the Factors of a number using For Loop

# Python Program to find Factorial of a Number

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 %d  = %d" %(number, fact))
Python Program to find Factorial of a Number 2

User entered integer in the above Python example is 4

First Iteration
i = 1, Fact = 1 and number = 5

Fact = Fact * i;
Fact = 1 * 1 = 1

Second Iteration
i = 2, Fact = 1 and Number = 5
Fact = 1 * 2 = 2

Third Iteration
i = 3, Fact = 2 and Number = 5
Fact = 2 * 3 = 6

Fourth Iteration
i = 4, Fact = 6 and Number = 5
Fact = 6 * 4 = 24

Next, i become 5. So, For loop Terminated.

Python Program to find Factorial of a Number using While Loop

In this python factorial program, we just replaced the for loop with While Loop

# Python Program to find Factorial of a Number

number = int(input(" Please enter any Number to find factorial : "))
fact = 1
i = 1

while(i <= number):
    fact = fact * i
    i = i + 1

print("The factorial of %d  = %d" %(number, fact))
Python Program to find Factorial of a Number 3

Python Program to find Factorial of a Number using Functions

This factorial program is the same as the first example. However, we separated the logic using Functions

# Python Program to find Factorial of a Number

def factorial(num):
    fact = 1

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

    return fact


number = int(input(" Please enter any Number to find factorial : "))

facto = factorial(number)
print("The factorial of %d  = %d" %(number, facto))
Python Program to find Factorial of a Number 4

Python Program to find Factorial of a Number using Recursion

This program for factorial of a number pass user entered value to the Function. Within this function, this program finds the Factorial of a number Recursively.

# Python Program to find Factorial of a Number

def factorial(num):
    if((num == 0) or (num == 1)):
        return 1
    else:
        return num * factorial(num - 1)


number = int(input(" Please enter any Number to find factorial : "))

fact = factorial(number)
print("The factorial of %d  = %d" %(number, fact))
Python Program to find Factorial of a Number 5

Within the user-defined function of this python factorial program, If Else Statement check whether the Number is Equal to 0 or 1

  • If the condition is TRUE, then the function returns 1.
  • If the condition is False, the function returns Number * (Number -1) recursively.

User Entered Value = 6

Factorial = Number * Factorial (Number -1);
= 6 * Calculate_Factorial (5)
= 6 * 5 * Calculate_Factorial (4)
= 6 * 5 * 4 * Calculate_Factorial (3)
= 6 * 5 * 4 * 3 * Calculate_Factorial (2)
= 6 * 5 * 4 * 3 * 2 * Calculate_Factorial (1)
= 6 * 5 * 4 * 3 * 2
= 720

Placed Under: Python Examples

  • Python Hello World Program
  • Python add 2 numbers Program
  • Python Arithmetic Operations
  • Python Calendar Example
  • Python Cube of a Number
  • Python Calculate Electricity Bill
  • Python Calculate Simple Interest
  • Python Compound Interest
  • Python Largest of Two Numbers
  • Python Largest of 3 numbers
  • Python Print Natural Numbers
  • Python natural numbers reverse
  • Python Leap Year Program
  • Python Odd or Even Program
  • Python Even Numbers 1 to N
  • Python Odd Numbers 1 to N
  • Python Positive or Negative num
  • Python Profit or Loss Program
  • Python Square of a Number
  • Python Square root of a Number
  • Python Number Divisible by 5, 11
  • Python Find Power of a Number
  • Python Print Multiplication Table
  • Python Quadratic Equation roots
  • Python Student Grade Program
  • Python Total, Average, and Percentage of 5 Subjects
  • Python Sum of G.P Series
  • Python Sum of A.P Series
  • Python Sum of Series 1³+2³+.+n³
  • Python Sum of Series 1²+2²+.+n²
  • Python Natural num Sum & Avg
  • Python Sum of N natural nums
  • Python Sum of Odd Numbers
  • Python Sum of Even Numbers
  • Python Sum of Even & Odd
  • Python Armstrong number
  • Python Count Digits in a Number
  • Python Fibonacci Series program
  • Python Factorial of a Number
  • Python Factors of a Number
  • Python First Digit of a Number
  • Python GCD of Two Numbers
  • Python Strong Number Program
  • Python Prime Number Program
  • Python Prime Numbers 1 to 100
  • Python LCM of Two Numbers
  • Python natural number in reverse
  • Python Palindrome Program
  • Python Palindrome nums 1-100
  • Python find Perfect Number
  • Python Prime Factors of Number
  • Python Reverse number program
  • Python Strong Number Program
  • Python Strong Numbers 1 to 100
  • Python Sum of Digits of Number
  • Python Swap Two Numbers
  • Python Alphabet or not Program
  • Python Alphabet or Digit
  • Python Digit or not program
  • Python Lowercase or not
  • Python Uppercase or not
  • Python Lowercase or Uppercase
  • Python Vowel or Consonant
  • Python Alphabet digit or special
  • Python ASCII Value of Character
  • Python ASCII String Chars
  • Python Concatenate Strings
  • Python Convert String to Upper
  • Python Convert String to Lower
  • Python Copy a String Program
  • Python Count string words using Dictionary
  • Python Count Alphabets Digits and Special Characters in String
  • Python String Count Vowels and Consonants
  • Python Count Vowels in a String
  • Python Count total string chars
  • Python Count Char Occ in String
  • Python Count Total String words
  • Python Last Char Occur in String
  • Python First Char Occur in String
  • Python String Find All Char Occur
  • Python Palindrome String
  • Python Print String Characters
  • Python Replace Blank Space with Hyphen in a String
  • Python Replace String character
  • Python remove Odd string Chars
  • Python String Remove Odd Index Chars
  • Python Remove Last Char Occurrence in a String
  • Python Remove First Char Occurrence in a String
  • Python Reverse a String Program
  • Python String Length Program
  • Python Toggle String Char Case
  • Python List Arithmetic Operation
  • Python Program to Add two Lists
  • Python Count List +Ve & -Ve num
  • Python Even & Odd List nums
  • Python 2nd Largest List Number
  • Python Large & Small List Num
  • Python Largest Number in a List
  • Python List Length
  • Python List Negative Numbers
  • Python List Positive Numbers
  • Python Odd Numbers in a List
  • Python Even Numbers in a List
  • Python Print Elements in a List
  • Python Put Positive and Negative Numbers in Separate List
  • Python Program to Put Even and Odd Numbers in Separate List
  • Python Program to Reverse List
  • Python Sort List in Ascending
  • Python Smallest Number in a List
  • Python Sum of List Even & Odd
  • Python Sum of List Elements
  • Python add key-valuepair to Dict
  • Python Map 2 lists to dictionary
  • Python Create Dictionary of Numbers 1 to n in (x, x*x) form
  • Python Create Dictionary of keys and values are square of keys
  • Python key exists in Dictionary
  • Python remove dictionary Key
  • Python multiply dictionary items
  • Python Sum of Dictionary Items
  • Python Merge Two Dictionaries
  • Python Area Of Circle
  • Python Circle Area, Diam Circumf
  • Python Area Of a Triangle
  • Python Area of Tri-base,height
  • Python Area of a Trapezoid
  • Python Equilateral Triangle area
  • Python Area of a Rectangle
  • Python Area of Rect use len,width
  • Python right angle triangle area
  • Python Cylinder Vol & Surf Area
  • Python Cube Volume & Surface
  • Python Cone Volume & Surface
  • Python Cuboid volume, surface
  • Python check Triangle is Valid
  • Python Print Floyd’s Triangle
  • Python Invert Right Triangle Star
  • Python Program for Bubble Sort
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy