이 문제는 팩토리얼 문제로 재귀함수를 사용하면 반복문을 사용하지 않아도 쉽게 풀 수 있는 재귀함수의 기본적인 문제이다.
package recursion;
import java.util.*;
public class factorial {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int fac = factor(N);
System.out.println(fac);
}
public static int factor(int n) {
if(n == 0) return 1;
return n * factor(n-1);
}
}
- 재귀 함수의 간단한 문제를 풀어보면서 재귀 함수가 어떻게 동작이 되는지 공부할 수 있는 시간이었다.