In this post, you will learn a program to find the factorial of an N number using for-loop, recursion, while-loop, function, and so on but before writing a program you should know about the factorial number.

## What is a Factorial number?

Factorial of N number is defined as the product of all the numbers greater or equal to 1 and it is denoted by (N ! ).

For Example: 5! = 5*4*3*2*1 = 120

Now let’s write a Python program to find the factorial of N number by using different approaches.

## Python program to find Factorial of N number using while loop.

Before writing a program you should know about:

### Source code

``````num = int(input('Enter a number: '))
factorial = 1

i = 1
while i <= num:
factorial *= i
i = i + 1

print(f"Factorial of {num} is {factorial}")
``````

### Output

``````Enter a number: 7
Factorial of 7 is 5040``````

## Program to find Factorial of N number in python using for loop.

Before writing a program you should about:

1. for loop
2. Arithmetic Operators

### Source code

``````my_num = int(input('Enter a number: '))

facto = 1
for i in range(1,my_num+1):
facto *= i

print(f"Factorial of {my_num} is {facto}")
``````

### Output

``````Enter a number: 5
Factorial of 5 is 120``````

## Python program to find Factorial of N number using function.

Few things you should know before writing this program:

### Source code

``````def facto(n):
f = 1
for i in range(1,n+1):
f *= i
return f

num = int(input('Enter a number: '))

my_facto = facto(num)

print(f"factorial of {num} = {my_facto}")``````

### Output

``````Enter a number: 4
factorial of 4 = 24``````

We should also write above program using built-in function.

## Python program to find Factorial of N number using the built-in function.

there is one built-in function inside the math module called `factorial()` and using this function we can get easily factorial of any number

### Source code

``````from math import factorial

# print(factorial(5))
# print(factorial(7))
# print(factorial(6))

# ********* OR *********

my_num = int(input('Enter a number: '))
print(f'factorial of {my_num} = {factorial(my_num)}')``````

### Output

``````Enter a number: 3
factorial of 3 = 6``````

## Factorial of N number in python using recursion

``````def N_factorial(num):
if num <= 1:
return 1
else:
return num * N_factorial(num - 1)

number = int(input('Enter your number: '))
fac = N_factorial(number)

print(f'factorial of {number} = {fac}')``````

Output:

``````Enter your number: 8
factorial of 8 = 40320`````` Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

#### 1 Comment

1. Rusimbi William

Thank you for, can you do some algorithms of finding the Fibonacci sequence