6. แยกย่อย

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 6-18  เมท็อดหาผลบวก 1+2+...+n (แบบเรียกซ้ำ Sn = Sn-1+n เมื่อ n>0, S0 = 0)
public static int sum(int n) {
  if (n == 0) return 0;
  int s = sum(n-1) + n;
  return s;
}
©2009 S.Prasitjutrakul