- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program for factorial of a number
In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement −Our task to compute the factorial of n.
Factorial of a non-negative number is given by −
n! = n*n-1*n-2*n-3*n-4*.................*3*2*1
We have two possible solutions to the problem
- Recursive approach
- Iterative approach
Approach 1 −Recursive Approach
Example
def factorial(n): # recursive solution if (n==1 or n==0): return 1 else: return n * factorial(n - 1) # main num = 6 print("Factorial of",num,"is", factorial(num))
Output
('Factorial of', 6, 'is', 720)
All the variables are declared in global scope as shown in the image below
Approach 2 −Iterative Approach
Example
def factorial(n):# iterative solution fact=1 for i in range(2,n+1): fact=fact*i return fact # main num = 6 print("Factorial of",num,"is", factorial(num))
Output
('Factorial of', 6, 'is', 720)
All the variables are declared in global scope as shown in the image below
Conclusion
In this article, we learned about the approach to compute the factorial of a number n.
- Related Articles
- Program for factorial of a number in C program
- Python program to find factorial of a large number
- Python Program to Count trailing zeroes in factorial of a number
- Python Program to find the factorial of a number without recursion
- Swift Program to Find Factorial of a number
- Java Program to Find Factorial of a number
- Kotlin Program to Find Factorial of a number
- 8085 program to find the factorial of a number
- 8086 program to find the factorial of a number
- C++ program to Calculate Factorial of a Number Using Recursion
- C++ Program to Find Factorial of a Number using Iteration
- C++ Program to Find Factorial of a Number using Recursion
- Java Program to Find Factorial of a Number Using Recursion
- Factorial of a large number
- How to Find the Factorial of a Number using Python?

Advertisements