Lec: 17 | JAVA
LEC:17
Recursion in JAVA
Source Code:
package lec_5;
import java.util.*;
public class Lec_5 {
static int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
public static void main(String[] args) {
int hold = sum(10);
System.out.println("Sum is " + hold);
// int hold1 = 0;
// for (int i = 1; i < 11; i++) {
// hold1 += i;
// }
// System.out.println("Sum is " + hold1);
}
}
Comments
Post a Comment