Skip to content

Commit 766d883

Browse files
authored
Merge pull request #1 from SamratBarai/SamratBarai-quicksnip-factorial
Create Calculate-factorial.md
2 parents 8df9bc3 + e295e2f commit 766d883

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Calculate Factiorial of a number
3+
description: Calculates factorial of a given number using recursive function
4+
author: SamratBarai
5+
tags: math,factorial,recursive-function
6+
---
7+
8+
```py
9+
def factorial(n):
10+
if type(n) != int or n < 0: raise ValueError("Invalid value of input: '" + str(n) + "'") # Raises an error for invalid input
11+
if n == 0 or n == 1: return 1 # Returns 1 if n is 0 or 1
12+
else: return n * factorial(n-1) # Recall the factorial function
13+
```
14+
15+
# Usage:
16+
print(factorial(4)) # Returns 24
17+
print(factorial(-3)) # Returns type error for invalid inputs

0 commit comments

Comments
 (0)