Factorial Number in Python with Algorithm

Python Program to Find the Factorial of a Number

Aim:

To print the factorial number in Python Program

Algorithm1:

  1.  step : Start
  2. step : Declare the variable n and read n value 
    1. n = input(‘ enter the number ‘)
  3. step : Initialise fact = 1 and i = 1
  4. step : In while loop perform 
    1. fact = fact * i
    2. i = i + 1
  5. step : print factorial of n
  6. step : end

Example1: Calculate the Factorial of a Number Source Code:

n = int(input('enter the number :'))

fact1 = 1

i = 1

while n >= i:

fact2 = fact1 * i

i = i + 1

print(f"factorial number of n is {fact2}")

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter the number :10
factorial number of n is 1
factorial number of n is 2
factorial number of n is 3
factorial number of n is 4
factorial number of n is 5
factorial number of n is 6
factorial number of n is 7
factorial number of n is 8
factorial number of n is 9
factorial number of n is 10

Process finished with exit code 0

Algorithm2:

  1. Start
  2. import math or you can directly use import math.factorial
  3. print any message
  4. Then print(math.factorial(number))

Example2: Find Factorial of a Number using math.factorial() Method

import math
print('The factorial of 22 is: ', end='')
print(math.factorial(22))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
The factorial of 22 is: 1124000727777607680000

Process finished with exit code 0

FAQ:

What is factorial number in Python?

The factorial number is such a number that starts from 1 because factorial of 0! is 1 and the formula to find the factorial number is n! = n*(n-1)*(n-2)*(n-3)*….*3*2*1

How do you do Factorials in Python?

You can use this formula to do factorial of any number i.e. n! = n*(n-1)*(n-2)*(n-3)*….*3*2*1. For example factorial of 5! = 5*4*3*2*1 =120

Does Python have factorial?

Many people don’t know that python has a simple way to print factorial of any number easily. For that, you have to import a package called import math and then you can call the factorial method. Example:
import math
print(‘the factorial of 22 is: ‘, end=”)
print(math.factorial(22))

How do you find the factorial of a number?

You can find the factorial of a number in a different way in python. The first and simple way is by importing a package called math. Example
import math
print(‘the factorial of 22 is: ‘, end=”)
print(math.factorial(22))

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment