Trong bài viết này, bạn sẽ học cách tìm thấy giai thừa của một số và hiển thị nó.
Nấp của một số là sản phẩm của tất cả các số nguyên từ 1 đến số đó.
Để tìm hiểu về hoạt động của đệ quy, hãy truy cập đệ quy Python.
The factorial of 7 is 5040
9Factorial of 5 is 120
2 Factorial of 5 is 120
4# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 Factorial of 5 is 120
1 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
7
Factorial of 5 is 120
8# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
0 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1
Factorial of 5 is 120
8120
0# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1
120
8120
0Factorial of 5 is 120
5# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
8120
8Factorial of 5 is 120
4Factorial of 5 is 120
7# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1The factorial of 7 is 5040
9Factorial of 5 is 120
2 Factorial of 5 is 120
4# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 Factorial of 5 is 120
1 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
7
Factorial of 5 is 120
8# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
0 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1
Factorial of 5 is 120
8120
0# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1
120
8120
0Factorial of 5 is 120
5# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
81. Phương pháp tiếp cận: & NBSP;
python3
The factorial of 7 is 5040
7 The factorial of 7 is 5040
8The factorial of 7 is 5040
9# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
0 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
2 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
3# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4__Factorial of 5 is 120
0# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 Factorial of 5 is 120
2Factorial of 5 is 120
3Factorial of 5 is 120
4Factorial of 5 is 120
5Factorial of 5 is 120
6Factorial of 5 is 120
7Factorial of 5 is 120
8Factorial of 5 is 120
9Factorial of 5 is 120
0Output:
Factorial of 5 is 120
Độ phức tạp về thời gian: O (N) Không gian phụ trợ: O (N)O(n)
Auxiliary Space: O(n)
2. Cách tiếp cận lặp:
Method1:
python3
The factorial of 7 is 5040
7 The factorial of 7 is 5040
8The factorial of 7 is 5040
9# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
0 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
2 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
3# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4__Factorial of 5 is 120
0# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 Factorial of 5 is 120
2Factorial of 5 is 120
3Factorial of 5 is 120
4Factorial of 5 is 120
5Factorial of 5 is 120
6Factorial of 5 is 120
7Factorial of 5 is 120
8Factorial of 5 is 120
9
Độ phức tạp về thời gian: O (N) Không gian phụ trợ: O (N)
The factorial of 7 is 5040
9Factorial of 5 is 120
3Factorial of 5 is 120
7Factorial of 5 is 120
8120
0# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1Factorial of 5 is 120
8120
4120
5# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1120
7120
8120
0Factorial of 5 is 120
5# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
8120
8Factorial of 5 is 120
4Factorial of 5 is 120
7# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1Factorial of 5 is 120
8# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
0 The factorial of 7 is 5040
00Factorial of 5 is 120
0# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 Factorial of 5 is 120
2Factorial of 5 is 120
3Factorial of 5 is 120
4Factorial of 5 is 120
5Factorial of 5 is 120
6Factorial of 5 is 120
7Factorial of 5 is 120
8Factorial of 5 is 120
9Factorial of 5 is 120
0Output:
Factorial of 5 is 120
Độ phức tạp về thời gian: O (N) Không gian phụ trợ: O (1) O(n)
Auxiliary
Space: O(1)
Method2:
Python3
The factorial of 7 is 5040
7 The factorial of 7 is 5040
8The factorial of 7 is 5040
9The factorial of 7 is 5040
15# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1The factorial of 7 is 5040
9The factorial of 7 is 5040
4 The factorial of 7 is 5040
20The factorial of 7 is 5040
21 The factorial of 7 is 5040
22Factorial of 5 is 120
5The factorial of 7 is 5040
24The factorial of 7 is 5040
25The factorial of 7 is 5040
26The factorial of 7 is 5040
21Factorial of 5 is 120
8The factorial of 7 is 5040
15Factorial of 5 is 120
5# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 The factorial of 7 is 5040
33The factorial of 7 is 5040
9# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
0 The factorial of 7 is 5040
36Factorial of 5 is 120
0# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 Factorial of 5 is 120
2Factorial of 5 is 120
3Factorial of 5 is 120
4Factorial of 5 is 120
5Factorial of 5 is 120
6The factorial of 7 is 5040
44Factorial of 5 is 120
8Factorial of 5 is 120
9Factorial of 5 is 120
0Độ phức tạp về thời gian: O (N) Không gian phụ trợ: O (1)
Factorial of 5 is 120
Độ phức tạp về thời gian: O (N) Không gian phụ trợ: O (1)O(n)
Auxiliary Space: O(1)
The factorial of 7 is 5040
7 The factorial of 7 is 5040
8
Python3
The factorial of 7 is 5040
7 The factorial of 7 is 5040
8The factorial of 7 is 5040
9The factorial of 7 is 5040
15# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1The factorial of 7 is 5040
9The factorial of 7 is 5040
4 The factorial of 7 is 5040
20The factorial of 7 is 5040
21 The factorial of 7 is 5040
22Factorial of 5 is 120
5The factorial of 7 is 5040
24The factorial of 7 is 5040
25The factorial of 7 is 5040
26The factorial of 7 is 5040
21Factorial of 5 is 120
8The factorial of 7 is 5040
15Factorial of 5 is 120
5# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 The factorial of 7 is 5040
33The factorial of 7 is 5040
80Factorial of 5 is 120
0Output:
Factorial of 5 is 120
The factorial of 7 is 5040
9# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
0 The factorial of 7 is 5040
36O(n)
Auxiliary Space: O(n)
Đầu ra
3. Một giải pháp dòng (sử dụng toán tử ternary): & nbsp;
The factorial of 7 is 5040
9# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
0 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
2 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
3# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4__math.factorial() function returns the factorial of desired number.Factorial of 5 is 120
0# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 Factorial of 5 is 120
2 math.factorial(x)
Factorial of 5 is 120
4 Factorial of 5 is 120
5Factorial of 5 is 120
6Factorial of 5 is 120
7Factorial of 5 is 120
8Factorial of 5 is 120
9
x: This is a numeric expression.
Độ phức tạp về thời gian: O (N) Không gian phụ trợ: O (N) factorial of desired number.
Python3
Vui lòng tham khảo đầy đủ bài viết về chương trình cho Factorial của một số để biết thêm chi tiết!
The factorial of 7 is 5040
7 The factorial of 7 is 5040
8The factorial of 7 is 5040
9The factorial of 7 is 5040
15# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4 # Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
1The factorial of 7 is 5040
9The factorial of 7 is 5040
4 The factorial of 7 is 5040
20The factorial of 7 is 5040
21 The factorial of 7 is 5040
22Factorial of 5 is 120
5The factorial of 7 is 5040
24The factorial of 7 is 5040
25The factorial of 7 is 5040
26The factorial of 7 is 5040
21Factorial of 5 is 120
4Factorial of 5 is 120
5Factorial of 5 is 120
6The factorial of 7 is 5040
44Factorial of 5 is 120
8Factorial of 5 is 120
9The factorial of 7 is 5040
80Factorial of 5 is 120
0Output:
Factorial of 5 is 120
Python3
The factorial of 7 is 5040
9# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
0 The factorial of 7 is 5040
36# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
8# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
4Factorial of 5 is 120
2Đầu ra
Factorial of 5 is 120
4# Python program to find the factorial of a number provided by the user
# using recursion
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# recursive call to the function
return (x * factorial(x-1))
# change the value for a different result
num = 7
# to take input from the user
# num = int(input("Enter a number: "))
# call the factorial function
result = factorial(num)
print("The factorial of", num, "is", result)
19Output
120
Độ phức tạp về thời gian: O (N) Không gian phụ trợ: O (1)O(n)
Auxiliary Space: O(1)
Lao động của một số nguyên dương (số) là tổng số nhân của tất cả các số nguyên nhỏ hơn số nguyên dương đó.Ví dụ, giai thừa 5 là 5 * 4 * 3 * 2 * 1 tương đương với 120.sum of multiplication of all the integers smaller than that positive integer. For example, factorial of 5 is 5 * 4 * 3 * 2 * 1 which equals to 120.