Skip to content

Commit 23f3637

Browse files
authored
Create Factorial of Number
The factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 is 1*2*3*4*5*6 = 720. Factorial is not defined for negative numbers, and the factorial of zero is one, 0! = 1.
1 parent d07b765 commit 23f3637

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Factorial of Number

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#APPROACH-1
2+
3+
def factorial(n):
4+
if n==0:
5+
return 1
6+
else:
7+
return n*factorial(n-1)
8+
a=int(input())
9+
print(factorial(a))
10+
11+
#APPROACH-2
12+
13+
# To take input from the user
14+
num = int(input())
15+
factorial = 1
16+
# check if the number is negative
17+
if num < 0:
18+
print("Sorry, factorial does not exist for negative numbers")
19+
elif num == 0:
20+
print("The factorial of 0 is 1")
21+
else:
22+
for i in range(1,num + 1):
23+
factorial = factorial*i
24+
print("The factorial of",num,"is",factorial)

0 commit comments

Comments
 (0)