forked from Zipcoder/PyPart4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.py
More file actions
40 lines (30 loc) · 676 Bytes
/
factorial.py
File metadata and controls
40 lines (30 loc) · 676 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Exercise 5
Factorials are used to count permutations.
Create a program called factorial.py.
Requirements
Given a number (x), determine the value of x!
Use recursion
x result
0! 1
1! 1 * 1 1
2! 2 * 1 2
3! 3 * 2 * 1 6
4! 4 * 3 * 2 * 1 24
5! 5 * 3 * 2 * 1 120
... ... ...
Constraints
n >= 0 n < 995
Answer below:
"""
def factorial(n):
if n > 1:
return (factorial(n - 1) * n)
elif n == 0 or n==1:
return 1
return (factorial(n))
n = int(input("Provide a number between 0 (included) and 995: "))
if n >= 0 and n < 995:
print('factorial of',n, 'is',factorial(n))
else:
print("You need to provide a valid input to run the function")