

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Factorial in Python
Suppose we have a number n less than or equal to 10, we have to find its factorial. We know that the factorial of a number n is n! = n * (n - 1) * (n - 2) * ... * 1.
So, if the input is like 6, then the output will be 720
To solve this, we will follow these steps −
- Define a function solve(). This will take n
- if n <= 1, then
- return 1
- if n <= 1, then
- return n * solve(n - 1)
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, n): if(n <= 1): return 1 return n * self.solve(n - 1) ob = Solution() print(ob.solve(6))
Input
6
Output
720
- Related Questions & Answers
- Three Different ways to calculate factorial in C#
- factorial() in Python
- Clumsy Factorial in Python
- Inverse Factorial in Python
- Write a C# program to calculate a factorial using recursion
- C++ program to Calculate Factorial of a Number Using Recursion
- How can we create MySQL stored procedure to calculate the factorial?
- Java program to calculate the factorial of a given number using while loop
- C# factorial
- Python Program for factorial of a number
- Factorial recursion in JavaScript
- Check if N is a Factorial Prime in Python
- How to Find Factorial of Number Using Recursion in Python?
- Recursive factorial method in Java
- Factorial Trailing Zeroes in C++
Advertisements