본문 바로가기

Java SE

Self Invocation

/* 재귀호출을 이용하여 1~10까지의 합을 구하는 예 */
public class SelfInvo2 {
 
 public static void main(String[] args) {
  System.out.println(test(10));
 }
 
 static int test(int n){
  if(n<1) return 0;
  return n+test(n-1);
 }
}