팩토리얼
-1부터 양의 정수 n까지의 정수를 모두 곱한 것
0! > 1 = 1
1! > 1 = 1
2! > 1X2 = 2
.
.
.
5! > 1X2X3X4X5 = 120
파이썬을 이용해서 팩토리얼 결괏값을 출력하는 프로그램 만들어 보기
반복문 이용
inputN = int(input('n 입력: '))
result = 1
for n in range(1, inputN + 1):
result *= n
print('{} 팩토리얼: {}'.format(inputN, result))
result = 1
n = 1
while n <= inputN:
result *= n
n += 1
print('{}팩토리얼: {}'.format(inputN, result))
재귀 함수 이용
inputN = int(input('n 입력: '))
def factorialFun(n):
if n == 1: return 1
return n * factorialFun(n -1)
print('{}팩토리얼: {}'.format(inputN, factorialFun(inputN)))