Factorial Program in Python
The factorial of a number n is denoted by n!. This is the product of all positive numbers less than or equal to n. It is calculated as-
n! = n X (n-1) X (n-3) X ……… X 3 X 2 X 1
The factorial operation generally comes across many mathematical operations, like- combinatorics, algebra, and analysis.
Example
Suppose we want to get factorial of 6, then factorial will be-
6! = 6 X 5 X 4 X 3 X 2 X 1
720
Python gets factorial using For Loop
Here is the Python program to find the factorial of a number using a for loop. When the user enters the number as input, it passes through the if else statement. If the number is greater than 0, then it passes through the for loop and finds the factorial of the number.
num = int(input("Enter a number : "))
factorial = 1
if num > 0:
for x in range(1,num + 1):
factorial = factorial*x
print("The factorial of",num," : ",factorial)
elif num == 0:
print("The factorial of 0 is 1")
else:
print("Factorial does not exist for given input")
Output of the above code
Enter a number: 6
The factorial of 6 : 720
Enter a number: 5
The factorial of 5 : 120
Enter a number: -1
Factorial does not exist for given input
Enter a number: 0
The factorial of 0 is 1
Python gets factorial using Recursion function
Here is the Python program to find the factorial of a number using the recursion function. When a user enters a number as input, it passes to the function. If the number equals 1, it returns 1. The factorial of a number less than equal to 0 does not exist. If the input is greater than 1, the function calls recursively and finds the factorial of a number.
def factorial(num):
if num == 1:
return num
elif num <= 0:
return " Not Exist"
else:
return num * factorial(num - 1)
# Enter the input
num = int(input("Enter a Number: "))
print("Factorial of", num, " : ", factorial(num))
Output of the above code
Enter a Number : 3
Factorial of 3 : 6
Enter a Number : 1
Factorial of 1 : 1
Enter a Number : 4
Factorial of 4 : 24
Enter a Number : 0
Factorial of 0 : Not Exist
Enter a Number : 6
Factorial of 6 : 720
Python gets factorial using while loop
In the given Python program, we ask the user to enter a number and store it in a variable num. The while loop continues till the value of num greater than 0. Then within the loop, the value of 'num' is multiplied with the value of 'fact'. Finally, we get the factorial value of the given number.
# Python program find factorial of a number
# using the while loop
num = int(input("Enter a positive number:"))
fact=1;
if num<0:
print("Factorial of negative numbers is not defined.");
elif(num==0):
print("The factorial of 0 is 1");
else:
while(num>0):
fact=fact*num
num=num-1
print("Factorial of the given number: ")
print(fact)
Output of the above code:
Enter a positive number:9
Factorial of the given number:
362880
Enter a positive number:11
Factorial of the given number:
39916800
Related Articles
Convert list to dictionary PythonDictionary inside list Python
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python add list to list
Python Pandas Dataframe to CSV
Python compare two lists
Remove element from list Python
How to draw different shapes using Python Pygame
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml