Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Python Factorial Program (How to Find Factorial of a Number in Python?)
Learn how to find factorial of a number in Python with different types of programs. All programs and examples are covered in simple terms with code, output, as well as explanations.
Factorial is a mathematical operation that finds the product of all positive integers up to a given number. It is commonly denoted by the exclamation mark (!).
For example, the factorial of 5 is calculated as:
5! = 5 x 4 x 3 x 2 x 1 = 120.
In this tutorial on Python factorial program, we will walk you through the step-by-step process of creating a program that can calculate the factorial of any given number. We'll cover the fundamental concepts and provide you with easy-to-understand code examples.
By the end of this tutorial, you will have a solid understanding of how to use Python's programming constructs such as loops and conditional statements to implement a factorial calculation algorithm. This knowledge will serve as a strong foundation for your future Python programming endeavors.
So, let’s get started and understand the factorial code in Python.
Python Factorial Program
Code
# Program to find factorial of a number in Python using function
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# Taking input from the user
num = int(input("Enter a number: "))
# Checking if the number is negative
if num < 0:
print("Factorial cannot be calculated for negative numbers.")
elif num == 0:
print("Factorial of 0 is 1.")
else:
result = factorial(num)
print("Factorial of", num, "is", result)
Output
Enter a number: 10
Factorial of 10 is 3628800
Explanation
-
In this program, we define a recursive function called factorial that calculates the factorial of a given number n.
-
The function checks if n is equal to 0, in which case it returns 1 (since the factorial of 0 is defined as 1). Otherwise, it recursively calls itself with n-1 and multiplies the current number n with the result.
-
We then take input from the user using the input function and convert it to an integer using int().
-
We check if the number is negative, in which case we display an appropriate error message.
-
If the number is zero, we print that the factorial is 1. Otherwise, we call the factorial function with the given number and store the result in the result variable. Finally, we print the factorial of the number.
Factorial of a Number in Python Using for loop
Code
# Factorial in Python using for loop
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
# Taking input from the user
num = int(input("Enter a number: "))
# Checking if the number is negative
if num < 0:
print("Factorial cannot be calculated for negative numbers.")
elif num == 0:
print("Factorial of 0 is 1.")
else:
result = factorial(num)
print("Factorial of", num, "is", result)
Output
Enter a number: 5
Factorial of 5 is 120
Explanation
-
Here, we define a function called factorial that takes a number n as input and uses a for loop to iterate from 1 to n.
-
Within each iteration, we update the result variable by multiplying it with the current value of i. This way, we gradually calculate the factorial of the given number using for loop.
-
We take input from the user, check for negative numbers, and display the factorial accordingly.
-
By using a for loop, we avoid recursion and directly calculate the factorial by multiplying the numbers in the loop.
This approach is especially useful when dealing with larger numbers, as it reduces the chances of exceeding recursion limits.
Python Factorial Program Using while loop
Code
# How to find factorial of a number in Python using while loop
def factorial(n):
if n == 0:
return 1
else:
result = 1
while n > 0:
result *= n
n -= 1
return result
# Taking input from the user
num = int(input("Enter a number: "))
# Checking if the number is negative
if num < 0:
print("Factorial cannot be calculated for negative numbers.")
elif num == 0:
print("Factorial of 0 is 1.")
else:
result = factorial(num)
print("Factorial of", num, "is", result)
Output
Enter a number: 0
Factorial of 0 is 1.
Explanation
-
In this Python factorial program, we define a function called factorial that takes a number n as input. Inside the function, we first check if n is equal to 0. If so, we return 1 since the factorial of 0 is defined as 1.
-
If n is not 0, we initialize a variable called result to 1. We then use a while loop to repeatedly execute the block of code as long as n is greater than 0.
-
Within each iteration, we update the result variable by multiplying it with the current value of n, and then decrement n by 1. This way, we gradually calculate the factorial of the given number.
Using a while loop in this program provides an alternative approach to find the factorial of a number in Python, iterating until the number reaches 0. This can be especially useful in scenarios where you prefer a while loop over a for loop.
Factorial Program in Python Using Function Without Recursion
Code
# Python program for factorial of a number without recursion
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
# Taking input from the user
num = int(input("Enter a number: "))
# Checking if the number is negative
if num < 0:
print("Factorial cannot be calculated for negative numbers.")
elif num == 0:
print("Factorial of 0 is 1.")
else:
result = factorial(num)
print("Factorial of", num, "is", result)
Output
Enter a number: 12
Factorial of 12 is 479001600
Explanation
-
We define a function called factorial that takes a number n as input. Inside the function, we initialize a variable called result to 1.
-
Then, we use a for loop to iterate from 1 to n, and in each iteration, we update the result variable by multiplying it with the current value of i. This way, we gradually calculate the factorial of the given number.
-
We follow the same steps as before to take input from the user, check for negative numbers, and display the factorial accordingly.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program