대단히 쉬운 문제지만.. C언어 학습을 위해서 여러 방법으로 풀어보았다. => 근데 역시 매우 쉬운..문제임. 그러나 어디든 도움이 되겠지?!
① 단순 반복문으로 풀어가기
#include <stdio.h>
int main(void){
int N, ans = 1;
scanf("%d",&N);
for (int i = 1; i<=N; i++){
ans *= i;
}
printf("%d",ans);
}
② 재귀 활용하기
#include <stdio.h>
int factorial(int a){
if (a == 0 || a == 1)
return 1;
else
return a*factorial(a-1);
}
int main(void){
int N;
scanf("%d",&N);
printf("%d",factorial(N));
}
'Algorithm' 카테고리의 다른 글
[백준] 10816번-숫자카드2 (Python) -> Counter함수 (작성중) (1) | 2022.07.05 |
---|---|
[백준] 9020번-골드바흐의 추측 (Python) -> 소수찾기의 극한 (0) | 2022.07.04 |
[백준] 4948번- 베르트랑 공준 (Python) (0) | 2022.07.04 |
[백준] 1193번-분수찾기 (Python) (0) | 2022.06.23 |
[백준] 1157번- 단어 공부 (Python) (0) | 2022.05.09 |