코딩테스트/백준 알고리즘
[백준 알고리즘] 10872번 팩토리얼 파이썬
딥런
2022. 2. 25. 02:05
팩토리얼
문제 설명
0보다 크거나 같은 정수 N이 주어진다. 이때, N!을 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 정수 N(0 ≤ N ≤ 12)이 주어진다.
출력
첫째 줄에 N!을 출력한다.
Python 코드
def factorial(n):
result = 1
if n > 0:
result = n * factorial(n-1)
return result
n = int(input())
print(factorial(n))
* 참고 링크 : https://ooyoung.tistory.com/114
C++ 코드
#include <cstdio>
int factorial(int n)
{
if(n <= 1)
return 1;
return n * factorial(n-1);
}
int main() {
int num;
scanf("%d",&num);
printf("%d",factorial(num));
}
* 참고 링크 : https://cryptosalamander.tistory.com/36
출처