Skip to content

Commit 5d796fa

Browse files
committed
Create ExerciseCh6
1 parent e780f0a commit 5d796fa

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

ch06/ExerciseCh6

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class ExerciseCh6 {
2+
3+
public static void main(String[] args) {
4+
int n = 5;
5+
oddSum(n);
6+
System.out.println("The sum of integers from 1 to n is " + oddSum(n));
7+
}
8+
9+
public static int oddSum(int n) {
10+
if (n == 0) {
11+
return 0;
12+
} else if
13+
(n % 2 != 0) {
14+
return n + oddSum(n - 1);
15+
} else {
16+
return oddSum(n - 1);
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)